Linux and MacOS – How to List Files Recursively and Sort by Modification Time

linuxlsmacos

How do I list all files under a directory recursively and sort the output by modification time?

I normally use ls -lhtc but it doesn't find all files recursively. I am using Linux and Mac.

ls -l on Mac OS X can give

-rw-r--r--    1 fsr  user      1928 Mar  1  2011 foo.c
-rwx------    1 fsr  user      3509 Feb 25 14:34 bar.c

where the date part isn't consistent or aligned, so a solution have to take this into account.

Partial solution

stat -f "%m%t%Sm %N" ./* | sort -rn | head -3 | cut -f2-

works, but not recursively.

Best Answer

Here is a method using stat as @johnshen64 suggested

find . -type f -exec stat -f "%m%t%Sm %N" '{}' \; | sort -rn | head -20 | cut -f2-
Related Question