Score:0

Open a file based on its modification date

cn flag

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

Score:2
hr flag

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])'
Rogo avatar
cn flag
I left out a crucial piece of information. The most recent file has spaces so it appear in the directory as 'file name.odt'. Also, I do not use zsh. I use bash. I tried a number of variations with no success so far.
hr flag
@Rogo the above method should work for any legal file name (including those with spaces and even newlines)
Rogo avatar
cn flag
Installed zsh and it worked!! Thanks
Rogo avatar
cn flag
Although it worked and I am glad for that, there were two lines of error messages in the terminal after the execution. I can ignore them but prefer to improve my code/understanding. Is there a place I can go to decipher this? Here are the msgs func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
hr flag
@Rogo that would be a message from LibreOffice itself, rather than from the shell command. See for example [Error running Libreoffice 6.0.5. Did ubuntu 18.04 fix itself after I looked up the version number?](https://askubuntu.com/a/1108733/178692)
Rogo avatar
cn flag
you helped me on this some months ago and it works fine for I have used it a lot since. But I made it into a script so I could just execute ./tasksheet and it works like a macro. Now I can't find the script. I doubt I put it anywhere other than wherever linux defaults to. Can you guess where it is? Thanks
Score:1
cn flag

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)"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.