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

13

3

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.

qazwsx

Posted 2012-04-24T18:37:09.113

Reputation: 6 739

I'm going to assume you are on Linux, but you should specify your OS explicitly either in the question or preferably in as a tag. – EBGreen – 2012-04-24T18:44:22.560

Answers

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-

Computist

Posted 2012-04-24T18:37:09.113

Reputation: 2 341

which version of stat are you using? your format above didn't work for me. I had to use the following: find . -type f -exec stat --printf "%Y\t%y %N\n" '{}' \; | sort -rn | cut -f2-. This is with stat (GNU coreutils) 8.23 – user84207 – 2017-09-04T02:23:03.430

find "$PWD" -type f -exec stat -f $'%m\t%N' '{}' \; | sort -rn | cut -f2- would print just the absolute paths of the files on OS X. – Lri – 2012-04-26T09:04:44.307

12

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

Daniel Andersson

Posted 2012-04-24T18:37:09.113

Reputation: 20 465

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.

johnshen64

Posted 2012-04-24T18:37:09.113

Reputation: 4 399

i also assume it is linux, like EBGreen said, ls is an alias in windows PS so you need to use sort-object and get child items recursively if that is the case but the idea is the same. – johnshen64 – 2012-04-24T18:53:46.533

Does it really work correctly when the dates sometimes misses the year part, on Mac? – qazwsx – 2012-04-24T19:10:27.807

you are right, the date display for recent files will mess up things. you need to use stat instead to get the exactly formatted date in that case. my files are all very old so the command worked fine, that is why i added the tune clause :-) – johnshen64 – 2012-04-24T19:16:33.230

@johnshen64: ls on Linux has a --time-format option you can use to ensure that both recent and older file dates are shown in the same format. – RedGrittyBrick – 2012-04-24T19:46:37.450

ah thanks, you are right, just have not used that. that is much simpler than using stat. it is called --time-style i think. – johnshen64 – 2012-04-24T21:30:36.673

0

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

Brad Parks

Posted 2012-04-24T18:37:09.113

Reputation: 1 775

This is really a comment and not an answer to the original question. You can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. Please read Why do I need 50 reputation to comment? What can I do instead?

– DavidPostill – 2018-01-14T21:46:42.430