How to read a file name that was recently edited when listed using ls command?

I was trying to write a script that will process recently creatd file.
From below, the script should process input_20111230.dat file.

sam:/top/work/data/input: ls -ltr input_*.dat
-rw-rw-rw- 1 work edprod 455668 Dec 24 09:16 input_20111224.dat
-rw-r--r-- 1 work edprod 7344672369 Dec 30 00:45 input_20111230.dat

Please help me as how to extract file name alone(input_20111230.dat) from the above and pass it to the script as input.

Thanks in advance...

[LEFT]If you're talking about the recently modified file, then try this:

ls -1t input_*.dat | head -1

[/LEFT]

1 Like

Thanks balajesuri...

From my analysis i guess, ls -ltr command list files such that recently created file gets listed at the end.
For example, input_20111230.dat ----> file created on 30 Dec

I guess ls -1t input_*.dat | tail -1 will solve my purpose.
Correct me if I am wrong...

ls -1t

highlighted is not l its 1 (numerica one)

1 Like

By the way someone help me as how to extract only the file name(input_20111230.dat) from the ls -ltr command output.

sam:/top/work/data/input:ls -ltr input_*.dat | tail -1
-rw-r--r-- 1 work edprod 7344672369 Dec 30 00:45 input_20111230.dat

I need the file name alone to be sent as input to a script. Thanks in advance...

ls -ltr input_*.dat | tail -1 | awk '{print $NF}'

---------- Post updated at 10:09 AM ---------- Previous update was at 10:08 AM ----------

ls -ltr input_*.dat | nawk '{a=$NF}END{print a}'
1 Like

@siteregsam: I don't understand. Why would you want to go for a long listing (ls -l) and then truncate just the file name from it?

Didn't ls -1t input_*.dat | head -1 work?

$ ls -1t input_*.dat
input_20111230.dat
input_20111224.dat
$
$ ls -1t input_*.dat | head -1
input_20111230.dat

@@balajesuri: Sorry yaar, I used ls -ltr, so it listed as below

 
$ ls -1t input_*.dat
input_20111224.dat
input_20111230.dat