Linux – How to sort results of Find statement by date

filesfindlinuxsort

Trying to find files that contains specific strings in name, but don't know how to sort output, in a way, that I will get file names only.

I've tried

OLDDATA=`find . -regex ".*/[0-9.]+" | ls -t`

But ls -t is not working on find result but on whole directory

edit: Result of this statement should be sorted by modification day directories. This regex suppose to match directories that contains only numbers and dots in name.

Best Answer

get file names only ... sorted by modification day

find + sort + cut approach:

find . -regex ".*/[0-9.]+" -printf "%T@ %f\n" | sort | cut -d' ' -f2

  • %T@ - File's last modification time, where @ is seconds since Jan. 1, 1970, 00:00 GMT, with fractional part

  • %f - File's name with any leading directories removed (only the last element)


To sort in descending order:

find . -regex ".*/[0-9.]+" -printf "%T@ %f\n" | sort -k1,1r | cut -d' ' -f2
Related Question