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
:
%Tk
File's last modification time in the format specified byk
, which is the same as for%A
.- where
k
in 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
%p
File's name.
-
8
-
9
ls -t
sorts newer to older,sort
sorts older to newer. Sols -t
's reverse order issort
's normal order.– angusJan 24, 2012 at 15:19 -
1
-
6
-
8To get this to work with OSX, install findutils from homebrew, then use gfind not find. stackoverflow.com/questions/752818/…– ChrisMar 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. 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 3 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 ton
withls ... * */* */*/* ...
Feb 14, 2017 at 9:26 -
1
sudo find / -printf "%T+ %p\n" | grep -v "/proc/" | grep -v "/sys/" | sort | less +G
(I removeproc
andsys
on purpose here).