where the '$9' in the awk statement represents the ninth (last) field in the ls -l outputline. This is the filename on many UNIX / Linux systems. By using ls -ltr the listing is sorted on t(ime)r(eversely).
BTW. This is Linux syntax, if it doesn't work on your system you always can use `` instead of $().
The files name is only example... The problem is I have lot of file name and I don't want to specify in my scripts what is the name of the files. Whatever files are there , I just want to pick up the lastest one if duplicate. So I can not hard coded the file name there. Is there a way to get rid of the datestamp first then apply the ls -ltr command to it to get the lastest file. Thank you.
for i in $(ls -tr *[0-9][0-9][0-9][0-3][0-9]*); do
cp $i somelocation/$(echo $i | sed 's/^\(.*[^0-9]\)[0-9][0-9][0-9][0-3][0-9]\([^0-9].*\)$/\1\2/')
This is a very basic algorithm for detecting files with a datevalue in their names (yyyymmdd). It cuts out the datevalue from every filename found and writes the original files to a file with the stripped name.
E.g. every a_YYYYMMDD.dat file will be written to 'a_.dat' and every b_YYYYMMDD.dat file to 'b_.dat'. Because the filenames are fetched with 'ls -tr' (adopted from pdreyer, didn't know it would work without the long list...) the files are reversely sorted by timestamp and thus overwrite the previous (= older) file with the target filename.