The Computer Oracle

How to list files recursively and sort them by modification time?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2

--

Chapters
00:00 How To List Files Recursively And Sort Them By Modification Time?
00:39 Answer 1 Score 0
00:54 Accepted Answer Score 2
01:06 Answer 3 Score 16
01:45 Answer 4 Score 1
01:58 Thank you

--

Full question
https://superuser.com/questions/416308/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #macos #ls

#avk47



ANSWER 1

Score 16


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).




ACCEPTED ANSWER

Score 2


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-



ANSWER 3

Score 1


This answer to a similar question on the Unix Stack Exchange site helped me because I was using zsh:

How to list files sorted by modification date recursively (no stat command available!)




ANSWER 4

Score 0


find . should be able to get all the files. Something like this:

find . -exec ls -dl '{}' \; | sort -k 6,7

You need to tune it for you needs.