I have a huge set of(more than 300) log files(nmn_log.*.txt) in one of my logdir. I had to find out the latest log file name.
$ ls -lrt
total 475244
-rw-r--r-- 1 jks staff 13464 May 20 06:34 nmn_log.23219.txt
-rw-r--r-- 1 jks staff 96851 May 20 06:38 nmn_log.23236.txt
...
...
-rw-r--r-- 1 jks staff 96969 Jun 4 11:59 nmn_log.23233.txt
-rw-r--r-- 1 jks staff 91016 Jun 4 12:03 nmn_log.23239.txt
drwxr-xr-x 2 jks staff 4096 Jun 4 12:04 old
The intention is to print the last field of the last line (except the dir old) from the "ls -lrt" output in that directory.
$ ls -lrt | awk '/nmn_log/ { f=$NF };END{ print f }'
nmn_log.23239.txt
So basically the general way of printing the last field of last line(in this case the last field of last line of "ls -lrt" command):
$ ls -lrt | awk '{ f=$NF }; END{ print f }'
4 comments:
Can
ls -t1 | head -n1
also do the trick ?
Paul, you are absolutely right. I did all the awk stuffs just to explain how we can print the last field of last line. Thanks for your comment. Keep reading my blog.
//Jadu
Don't forget that sometimes what you want is the more recently *used*, which "ls -lut" will give you.
Just another handy little thing..
To get the latest file in a directory tree, use this instead:
find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1
Post a Comment