search latest version of log file

Have checked the forums and couldnt locate help on this.

I want to grep a log file for a pattern using a script - I need to grep the latest log file and not sure how I am able to ensure I am greping the latest log file.

Here is sample of log files for yestersday and I effectively need to grep the file with version number 28 below - but obviously this version number could be different depending on time of day as it may have increased....

Any help?

-rw-r--r-- 1 user1 other 15925895 Sep 28 08:39 20080928_19.log
-rw-r--r-- 1 user1 other 15925032 Sep 28 08:54 20080928_20.log
-rw-r--r-- 1 user1 other 15928665 Sep 28 09:10 20080928_21.log
-rw-r--r-- 1 user1 other 15925819 Sep 28 09:25 20080928_22.log
-rw-r--r-- 1 user1 other 15927413 Sep 28 09:41 20080928_23.log
-rw-r--r-- 1 user1 other 15925002 Sep 28 09:56 20080928_24.log
-rw-r--r-- 1 user1 other 15927538 Sep 28 10:12 20080928_25.log
-rw-r--r-- 1 user1 other 15925392 Sep 28 10:27 20080928_26.log
-rw-r--r-- 1 user1 other 994628 Sep 28 10:50 20080928_27.log
-rw-r--r-- 1 user1 other 183662 Sep 28 23:59 20080928_28.log

Following can be used :

ll -ltr *.log| tail -1

ll -ltr *.log| tail -1
bash: ll: command not found

I am on Solaris 10 - is ll the right command?

check if ls-l command works for you..

yes ls -l is fine and ls -ltr is fine

However the return of ls -ltr | tail -1 is:
-rw-r--r-- 1 user1 other 183662 Sep 28 23:59 20080928_28.log

I still need to be able to just grep the file name found above as part of a script.
ie. I need to be able to grep pattern <latest_file>

Many ways to do this...one way...check this out...

ls -ltr | tail -1 | cut -d " " -f10
grep pattern $(ls -lrt | awk '{ file=$NF } END { print file }')

The value of $(command ...) is the output of command ...

If your shell doesn't support the $(...) syntax you will have to use `...` instead. Those are backticks (grave accents, ASCII 96) not regular straight quotes.

That works a treat - Thanks for your help.

FYI
The ls -ltr | tail -1 | cut -d " " -f10 doesnt work as the field separator is not unique - the length varies depending on the file size.

The -l option for ls command is unnecessary :

ls -tr | tail -1

Jean-Pierre.

Command we tried using to grep ERROR from lastest 1 log files from 5 logs files available in that particular directory is:
grep ERROR $(ls -lrt | awk '{ file=$NF } END { print file }')
grep: can't open DistributeImageFilesToTarget_10_dataLocations_PhillipinesDataLocations.xml.log

if i give ls -lrt for all the files in that diretory

-------r-- 1 egdevbb intdev 5022 Apr 2 05:13 DistributeDataFilesToTarget_4_dataLocations_ChinaDataLocations.xml.log
-------r-- 1 egdevbb intdev 1672 Apr 2 05:14 DistributeImageFilesToTarget_14_dataLocations_WHQDataLocations.xml.log
-------r-- 1 egdevbb intdev 7480 Apr 2 05:15 DistributeImageFilesToTarget_60_dataLocations_IndonesiaDataLocations.xml.log
-------r-- 1 egdevbb intdev 2113 Apr 2 05:34 DistributeImageFilesToTarget_8_dataLocations_MalaysiaDataLocations.xml.log
-------r-- 1 egdevbb intdev 2107 Apr 2 05:39 DistributeImageFilesToTarget_10_dataLocations_PhillipinesDataLocations.xml.log

(Note ls -one rather than ls -ell).

LATEST_FILENAME="`ls -1tr ????????_??.log 2>/dev/null | tail -1`"

if [ ! "${FILENAME}""X" = "X" ]
then
        grep <your pattern> "${LATEST_FILENAME}"
fi