LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-04-2004, 09:10 AM   #1
capella
LQ Newbie
 
Registered: Mar 2004
Posts: 3

Rep: Reputation: 0
Find newest file


I have a directory where a new file gets created every 2 hours, I need a way to find the newest file in that directory regardless of the filename, can someone please help me out.
 
Old 03-04-2004, 09:32 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
something like this?
find /mnt/backup -type f -name '*' -cmin -120 {} \;

This example takes an action on the files it finds, deletes them in this case.
find /mnt/backup -type f -name '*.gz' -mtime +90 -exec rm {} \;
 
Old 03-04-2004, 09:44 AM   #3
capella
LQ Newbie
 
Registered: Mar 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks

Thanks but I really need to find the newest file regardless of time created as there will be other backups moving in that directory, so the solution cannot be base on cmin etc..
 
Old 03-04-2004, 09:52 AM   #4
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Rep: Reputation: 47
How about doing an 'ls -lt' and piping that to a file and then parse the first line with a small script to get the filename.
 
Old 03-04-2004, 09:58 AM   #5
capella
LQ Newbie
 
Registered: Mar 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Yeah

I read somewhere that i can do that and combine it with tail-1, but i have no idea what that means im a database guy and was hoping someone could spoonfeed me the solution
 
Old 04-27-2012, 04:44 PM   #6
libCognition
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Rep: Reputation: Disabled
Lightbulb bash recursion

I just came up with a solution to this using recursion:
Code:
latest_file() {
  if [[ ! -z ${2} ]]
  then
    ls -l $2
    latest_file $1 $(find $1 -type f -newer "$2" | head -1)
  fi
}
Then run it with first argument being the folder to search, and the second argument being any arbitrary file in that folder. e.g. "latest_file /etc/ /etc/fstab | tail -n 1"

Limitations: probably doesn't work if a filename has spaces.
 
Old 04-27-2012, 10:25 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
ls -1rt --group-directories-first | tail -1
(that's a number 1 in -1rt)
 
Old 04-28-2012, 02:41 AM   #8
libCognition
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
Code:
ls -1rt --group-directories-first | tail -1
(that's a number 1 in -1rt)
That misses all the files that have a depth greater than zero.

This version of that approach would get some penetration:
Code:
ls -1rt --group-directories-first * */* */*/* | tail -1
but then you need to know how deep the structure goes. Or substitute with:
Code:
ls -1rt --group-directories-first $(find . -type d | sed 's/[^/]/*/g' | tr -s '*' | sort | uniq) | tail -1
What's interesting is the 'ls' version (above) got a different answer than 'find -newer' (from post 6), but both files were created the same minute. So one of the two approaches looks at the seconds.

Last edited by libCognition; 04-28-2012 at 02:58 AM.
 
Old 04-28-2012, 04:15 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by libCognition View Post
That misses all the files that have a depth greater than zero.
True but capella wrote in the OP "I have a directory" so its not a problem.
 
Old 04-28-2012, 04:26 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by libCognition View Post
What's interesting is the 'ls' version (above) got a different answer than 'find -newer' (from post 6), but both files were created the same minute. So one of the two approaches looks at the seconds.
That is interesting but experimentation showed that both find and ls are very fine grained regards time:
Code:
c@CW8:/tmp/tmp$  for i in $( seq 1 100 ); do touch $i; touch a$i; done
ls -lrt
[files listed in created order]
c@CW8:/tmp/tmp$ find . -newer 99
./100
./a100
./a99
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find newest files rookworm Linux - Newbie 4 10-28-2005 07:04 PM
Copying the newest file in a directory to another location Transition Linux - General 2 01-14-2005 11:53 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM
Find File broken, need search utility, where does WineX install, KDE file roller? Ohmn Mandriva 6 07-05-2004 10:34 PM
where can I find grammar file and lex file of GNU c ? thinks Huiming Linux - Software 1 06-11-2004 07:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:39 AM.

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