How to list files recursively and sort them by modification time?
Use find
's -printf
and sort on a reasonable date format:
find -type f -printf '%T+\t%p\n' | sort -n
This should minimize process forks and thus be the fastest.
Examples if you don't like the fractional second part (which is often not implemented in the file system anyway):
find -type f -printf '%T+\t%p\n' | sed 's/\.[[:digit:]]\{10\}//' | sort -n
find -type f -printf '%T+\t%p\n' | cut --complement -c 20-30 | sort -n
EDIT: Standard find
on Mac does not have -printf
. But it is not difficult to install GNU find on Mac (also see that link for more caveats concerning Mac/Linux compatibility and xargs
).
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-