grep latest file based on date.

hi all,

not sure if this has been posted b4 but i try to search but not valid.

this is my question:

when i do a ls -ltr there will be a list generated as follows:

-rw-r--r-- 1 root sys 923260 Jan 10 04:38 FilePolling.41025.083TL021.xml
-rw-r--r-- 1 root sys 1761337 Jan 10 04:40 FilePolling.41073.114TL020.xml
-rw-r--r-- 1 root sys 76 Jan 11 04:32 FilePolling.41423.024TL022.xml
-rw-r--r-- 1 root sys 82 Jan 11 04:40 FilePolling.41681.110TL022.xml
-rw-r--r-- 1 root sys 1136429 Jan 12 04:36 FilePolling.42235.068TL020.xml
-rw-r--r-- 1 root sys 82 Jan 12 04:40 FilePolling.42301.110TL022.xml
-rw-r--r-- 1 root sys 791807 Jan 13 04:31 FilePolling.42629.018TL024.xml

my question here is how do i grep the latest file (in this case the file will be the 13 Jan)? :confused:

can it be done?

thanks.

Simplest could be

ls -latr

If you looking for an xml file then, ls -latr | grep '.xml$'

grep "something" `ls -tr | tail -1`

Cheers
ZB

hi there,

thanks for the solution but dont think it's working.

what i meant is i want the latest file to be shown not the whole list of them ie:

-rw-r--r-- 1 root sys 791807 Jan 13 04:31 FilePolling.42629.018TL024.xml

Now I see what you want.

ls -latr | tail -1
-or-
ls -latr | sed -n '$p'

Cheers
ZB

I think the option a can be done away with. It would be ls -ltr | tail -1

hey guys,

its working...many thanks.

wee

It could be done away with if there is a 100% guarantee that a file will never be created such as

.filename.blah.xml

Anyway, in your original reply, you used the a too.

Adding an extra option to ls is not at all expensive, and is best practice to ensure that no files are missed.

Cheers
ZB

There might be cases like the following where 'a' will not help much.

[/tmp]$ ls -l unix
ls: unix: No such file or directory
[/tmp]$ touch unix
[/tmp]$ ls -latr | tail -2
-rw-r--r--    1 xxxxxxxx g900            0 Jan 17 03:09 unix
drwxrwxrwt   20 root     root         4096 Jan 17 03:09 .
[/tmp]$ ls -ltr | tail -1
-rw-r--r--    1 xxxxxxxx g900            0 Jan 17 03:09 unix

Yes, I did use 'a' before the above hit me ! :slight_smile:

Yeah but it wouldn't be rocket science for the original poster to see that it's returning the . or .. and then omit the a.

Anyway, to solve all this, he could use

ls -lAtr | tail -1

:smiley:

Cheers
ZB

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 -ltr | tail -1'

but this command is not working.. for me its saying.. cannot 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

The reason might be you do not have sufficient permissions to access that log file... Change the permissions using chmod and try your command

OR..

There may be other reason too...
Hope this should work..

grep ERROR `ls -ltr | tail -1|awk '{print $9}'`

Thanks
SHa