I am new to shell scripting so i need some help need how to go about with this problem.
I have a directory which contains files in the following format. The files are in a diretory called /incoming/external/data
AA_20100806.dat
AA_20100807.dat
AA_20100808.dat
AA_20100809.dat
AA_20100810.dat
AA_20100811.dat
AA_20100812.dat
As you can see the filename of the file includes a timestamp. i.e. [RANGE]_[YYYYMMDD].dat
What i need to do is find out which of these files has the latest date using the timestamp that is part of the filename not the system timestamp and move it to another directory and move the rest to a different directory.
system
August 16, 2010, 2:03pm
2
Assuming your RANGE is always AA
ls -1| tail -1 will give you the latest file.
Hi thanks for that. It seems to work but i cant work out how.
I looked at the manual for ls and it says that -1 "Prints one entry per line of output".
At what point does the sorting occur?
Scott
August 17, 2010, 5:48am
4
Hi.
The sort order of ls is alphabetical by default.
By using -1 it does print one entry per line, then tail -1 will take the last one (i.e. the newest by timestamp).
But you don't even need the -1:
$ touch AA_20100806.dat AA_20100807.dat AA_20100808.dat AA_20100809.dat AA_20100810.dat AA_20100811.dat AA_20100812.dat
$ ls
AA_20100806.dat AA_20100807.dat AA_20100808.dat AA_20100809.dat AA_20100810.dat AA_20100811.dat AA_20100812.dat
$ ls | tail -1
AA_20100812.dat
Oh ok i get it now. Thanks for you help.