I want to use find but sort the results reverse chronologically as with ls -ltr. Is this possible through any combo of flags or pipelines?
2 Answers
Use find's -printf command to output both the time (in a sortable way) and the file, then sort. If you use GNU find,
find . your-options -printf "%T+ %p\n" | sort
For convenience here is an explanation of the -printf "%T+ %p\n" from man find:
%TkFile's last modification time in the format specified byk, which is the same as for%A.- where
kin this case is set to+ +Date and time, separated by+, for example `2004-04-28+22:22:05.0'. This is a GNU extension. The time is given in the current timezone (which may be affected by setting the TZ environment variable). The seconds field includes a fractional part.
- where
%pFile's name.
-
9
-
9
ls -tsorts newer to older,sortsorts older to newer. Sols -t's reverse order issort's normal order.angus– angus2012-01-24 15:19:59 +00:00Commented Jan 24, 2012 at 15:19 -
1Do you have a version for OS X (non-gnu)?Ortomala Lokni– Ortomala Lokni2016-08-25 15:48:48 +00:00Commented Aug 25, 2016 at 15:48
-
6For OS X and non-GNU, use this answer.Tom Hale– Tom Hale2017-01-11 04:46:29 +00:00Commented Jan 11, 2017 at 4:46
-
9To get this to work with OSX, install findutils from homebrew, then use gfind not find. stackoverflow.com/questions/752818/…Chris– Chris2018-03-05 00:44:38 +00:00Commented Mar 5, 2018 at 0:44
If that is just a depth-n (assume depth-2) folder hierarchy, I find this one useful:
ls -laht --full-time */*
-
This seems to produce a list of files that are exactly two folders deep (no more, no less), along with separate listings of each of the folders that are exactly two folders deep.mwfearnley– mwfearnley2017-02-12 13:42:47 +00:00Commented Feb 12, 2017 at 13:42
-
2@mwfearnley that is exactly what I meant by "that is just a depth-n" above :) you can do
*/*/*if you want depth 3Ben Usman– Ben Usman2017-02-13 17:25:07 +00:00Commented Feb 13, 2017 at 17:25 -
2So basically, your suggestion only works as intended when all the files are exactly n levels deep, and there are no subfolders at that level. You should explain that. The latter might be surmountable by another flag for
ls, and you can perhaps cover all levels up tonwithls ... * */* */*/* ...mwfearnley– mwfearnley2017-02-14 09:26:48 +00:00Commented Feb 14, 2017 at 9:26 -
2May not work:
bash: /bin/ls: Argument list too longLuc– Luc2020-01-13 10:35:02 +00:00Commented Jan 13, 2020 at 10:35
sudo find / -printf "%T+ %p\n" | grep -v "/proc/" | grep -v "/sys/" | sort | less +G(I removeprocandsyson purpose here).