Shell Programming Blog Shell Programming Blog

Monday, 1 October 2012

How to find the latest file in a directory

Unknown | 07:21 |


Bash Linux Unix shell scripting

Suppose if you hava a directory with a huge set of files(more than 500) in a log directory, you would want to find the latest file in the directory. Now we can see few ways to that.

for example, Let us take the below scenario




1) Now our goal is to print the last field of the last line (except the dir old) from the "ls -lrt" output in that directory.




So basically to print the last field of last line(in this case the last field of last line of "ls -lrt" command), we can use the below command.

$ ls -lrt | awk '{ f=$NF }; END{ print f }'


2) This can also be acheived by using Head command.

$ ls -t1 | head -n1


3) This can also be acheived by using Find command.

$ find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1

0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming