Linux – Unix/Linux find and sort by date modified

findlinuxsortingunix

How can I do a simple find which would order the results by most recently modified?

Here is the current find I am using (I am doing a shell escape in PHP, so that is the reasoning for the variables):

find '$dir' -name '$str'\* -print | head -10

How could I have this order the search by most recently modified? (Note I do not want it to sort 'after' the search, but rather find the results based on what was most recently modified.)

Best Answer

Use this:

find . -printf "%T@ %Tc %p\n" | sort -n

printf arguments from man find:

  • %Tk: File's last modification time in the format specified by k.

  • @: seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.

  • c: locale's date and time (Sat Nov 04 12:02:33 EST 1989).

  • %p: File's name.

Related Question