LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find newest files in a directory (http://www.linuxquestions.org/questions/linux-newbie-8/find-newest-files-in-a-directory-639972/)

macushk 05-05-2008 04:02 AM

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

elthox 05-05-2008 04:12 AM

Quote:

Originally Posted by macushk (Post 3143093)
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

colucix 05-05-2008 04:16 AM

Code:

ls -t PROG_????????.log | head -1 | cut -c 6-13

b0uncer 05-05-2008 04:16 AM

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.


All times are GMT -5. The time now is 05:29 AM.