0

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

Rogo
  • 113

2 Answers2

2

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])'
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • 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. – Rogo Sep 23 '21 at 21:36
  • @Rogo the above method should work for any legal file name (including those with spaces and even newlines) – steeldriver Sep 23 '21 at 21:39
  • Installed zsh and it worked!! Thanks – Rogo Sep 23 '21 at 21:54
  • 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 – Rogo Sep 23 '21 at 22:17
  • @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? – steeldriver Sep 23 '21 at 22:53
  • 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 – Rogo Dec 23 '21 at 21:10
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)"
vanadium
  • 88,010