Thanks in advance
tail -2 return two file but I have to handle the film type there is two type in example STATE AND MEdd I want that code return one from state type and another from medd type on the basis of latest timestamp
I would guess that you wanted to include the date as well as the time in the sort anyway, so you may have accidentally gotten the date and timestamp you wanted.
If you need to sort on the date specified by the file names instead of by the files' timestamps, you might want something more like:
ls RATE_STATE* | sort -t '.' -k3,4nr | head -1
ls RATE_MEDD* | sort -t '.' -k3,4nr | head -1
If it is something you'll be doing frequently, you could replace two invocations of head , one invocation of ls , and two invocations of sort with one invocation of awk with something like:
ls RATE* | awk -F. '
{ if($3 > dt[$1] || ($3 == dt[$1] && $4 > ts[$1])) {
dt[$1] = $3
ts[$1] = $4
last[$1] = $0
}
}
END { for(i in last)
print last
}'
which, with your sample filenames, would produce the output: