LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-05-2008, 04:02 AM   #1
macushk
LQ Newbie
 
Registered: Oct 2006
Posts: 7

Rep: Reputation: 0
find newest files in a directory


Hi All,

I have some files with the names PROG_20080430.log ......PROG_20080501.log... in a directory. How can I use shell script to find the newest file and it's date !?

Many thanks
 
Old 05-05-2008, 04:12 AM   #2
elthox
Member
 
Registered: Oct 2006
Posts: 41

Rep: Reputation: 15
Quote:
Originally Posted by macushk View Post
Hi All,

I have some files with the names PROG_20080430.log ......PROG_20080501.log... in a directory. How can I use shell script to find the newest file and it's date !?

Many thanks
ls -lrt|grep ^PROG|tail -1|awk '{print $6" "$7" "$8" "$9}'

Assuming the is no other file that begins with PROG
 
Old 05-05-2008, 04:16 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
ls -t PROG_????????.log | head -1 | cut -c 6-13
 
Old 05-05-2008, 04:16 AM   #4
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
EDIT: nah, writing crap. Of course if the filenames are equally long and contain a date, ls sorts them by filename and thus by date. If you want to have the date separated (i.e. don't want to read it from the filename), use cut like in the above post.

You could also play around with hi-fi tools like find if you don't like it simple.

Last edited by b0uncer; 05-05-2008 at 04:20 AM.
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find files in current directory only (not recursively) babu198649 Linux - Newbie 2 12-27-2007 01:30 AM
Listing files in a directory without ls and find? szahri Linux - Newbie 11 03-23-2007 03:12 AM
How to find the date for all the files in a directory Uday123 AIX 6 02-23-2006 08:26 PM
How to find newest files rookworm Linux - Newbie 4 10-28-2005 07:04 PM
GTKMM - Compilation Errors - Can't find files or Directory sixtensixone Programming 3 04-06-2005 10:06 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration