get the last generated log file

Hi

I need to get the last generated file in a directory using ls -ltr. I need to store the output of ls -ltr in a variable.

it will like this

$xyz = -rw-rw-r-- 1 sblp003 siebel 1060 Dec 18 13:33

from this output, I need to do a substring to get this value alone "Dec 18 13:33". Can you guys please help me write a shell script to implement this logic.

I am not too familiar with shell scripting.

Thanks in advance

xyz=$(ls -ltr | grep ^- | tail -1 | awk ' { print $6,$7,$8 } ')

anbu

thanks for the reply. small modification to myy above requirement. I need to do ls -ltr for files starting with this 'eCustomerCME*'. Pls help me do this. thanks a lot!!

Try this, just change yoru "-" to "eCustomerCME" and take away the tail and awk

xyz=$(ls -ltr | grep ^eCustomerCME*)

i tried that, but is returning a blank result.. any other suggestions plz!

anbu

ur code is working exactly as i expected. thanks! just a small modification, it has to do a ls -ltr only for files starting with 'eCustomerCME'. I would basicall search for eCustomerCME*. thanks

xyz=$(ls -ltr eCustomerCME* | grep ^- | tail -1 | awk ' { print $6,$7,$8 } ')

Try this:

ls -ltr eCustomerCME* | awk '{if(NR>1) print $(NF-3),$(NF-2),$(NF-1)}'

The NR>1 is in place to ignore the first line of the '-l' output.

thanks a lot guys. bith ur solutions are working. i really appreciate ur help..