LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash - find newest file in a directory (http://www.linuxquestions.org/questions/linux-general-1/bash-find-newest-file-in-a-directory-828281/)

onesandzeros 08-24-2010 05:58 PM

bash - find newest file in a directory
 
Hello everyone,

Basically, I am trying to locate and copy the newest .json bookmark backup in my .mozilla/firefox/w987sdg9.default/bookmarkbackups directory.

I tried this
Code:

ls -t ~/.mozilla/firefox/b1ahb1ah.default/bookmarkbackups/ | head -1
which does return the newest file, but only the filename itself. I found readlink, but I haven't gotten that to output a full path which I can then feed to copy.

So, it seems to me that find might work well here, and I know how to find based on absolute dates, but not relative.

Is there a "good" way to do this? Thanks!

GrapefruiTgirl 08-24-2010 06:09 PM

This is interesting, and maybe hackish, but try this:
Code:


find /home/sasha -type f -iname $(ls -t ~/.mozilla/firefox/wu0fj5gp.Default/bookmarkbackups/ | head -1)

Just replace the bold part with your home directory path (like /home/onesandzeros)

Plus, you can use the find commands -exec option to run `cp` and do the copying for you, something like:
Code:

find /home/sasha -type f -iname $(ls -t ~/.mozilla/firefox/b1ahb1ah.Default/bookmarkbackups/ | head -1) -exec cp {} /destination/path \;
As I said - hackish :) but effective. Other better ways will likely follow.

Sasha

Kenny_Strawn 08-24-2010 06:21 PM

Or, easier:

Code:

find ~ -type f -iname $(ls -t ~/.mozilla/firefox/wu0fj5gp.Default/bookmarkbackups | head -1)
Just use the universal symbol for the home directory, a tilde. Or the $HOME variable, whichever you choose.


All times are GMT -5. The time now is 02:59 AM.