Open a file based on its modification date
I know how to open a LibreOffice writer file from the command line. I need to open the most recently modified file in the directory specified in path. It is an .odt file. Thanks
If you use zsh
in your terminal, you can use its glob qualifiers to select the youngest .odt
file (by modification time):
soffice path/to/dir/*.odt(om[1])
To use the zsh features from another shell, you could do
zsh -c 'soffice path/to/dir/*.odt(om[1])'
As long as the file name does not contain returns or other very strange characters, you can use ls -ct
to sort files most recently modified on top. You want to see only Libreoffice documents. You can use grep
to filter these. The first, which you can obtain with head -n 1
, will be the one you will want to open. You will open a file with the associated application using xdg-open
. If the shell variable mypath
contains the path to where your files reside, then following command will open the most recently modified .odt
document.
xdg-open "$mypath"/"$((cd "$mypath"; ls -ct) | grep -i '.odt$' | head -n 1)"