Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 08-24-2010, 05:58 PM   #1
LQ Newbie
Registered: Apr 2009
Posts: 3

Rep: Reputation: 0
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
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!
Old 08-24-2010, 06:09 PM   #2
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
This is interesting, and maybe hackish, but try this:
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:
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.


Last edited by GrapefruiTgirl; 08-24-2010 at 06:11 PM. Reason: EDIT - I omitted the trailing \;
Old 08-24-2010, 06:21 PM   #3
Senior Member
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
Or, easier:

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.


backup, bash, find

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to find newest files and count them buee Programming 1 02-25-2010 06:43 PM
Quickly find the newest file in a directory yorkshiresteve Linux - General 2 02-15-2009 11:32 AM
Script to find file differences in two directory trees (bash) Syqers Linux - Newbie 3 10-01-2008 11:55 PM
find newest files in a directory macushk Linux - Newbie 3 05-05-2008 04:16 AM
linux command error message bash: /usr/bin/find: No such file or directory sundaram123 Linux - General 8 04-02-2002 07:18 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:57 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration