111

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?

5
  • 1
    Best way is 'find <yadda yada> -exec ls -ltr {} +'.
    – gaoithe
    Mar 7, 2017 at 13:45
  • 4
    @gaoithe That works only up to a certain number of files. Beyond that, the files will be split in batches.
    – Dennis
    Mar 16, 2017 at 1:09
  • 2
    Voting to reopen because unix.stackexchange.com/questions/20611/… wants only in current directory "and subdirectories, which I don't want". Apr 8, 2019 at 10:19
  • Yeah, this isn't a duplicate at all. Even the answers of the two questions are distinct. Jun 4, 2020 at 13:51
  • To find all files on disk sorted by date, I use sudo find / -printf "%T+ %p\n" | grep -v "/proc/" | grep -v "/sys/" | sort | less +G (I remove proc and sys on purpose here).
    – Basj
    Sep 14, 2021 at 7:52

2 Answers 2

164

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 by k, 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.
  • %p File's name.
12
  • 8
    sort -r for reverse order.
    – stnly
    Jan 24, 2012 at 15:12
  • 9
    ls -t sorts newer to older, sort sorts older to newer. So ls -t's reverse order is sort's normal order.
    – angus
    Jan 24, 2012 at 15:19
  • 1
    Do you have a version for OS X (non-gnu)? Aug 25, 2016 at 15:48
  • 6
    For OS X and non-GNU, use this answer.
    – Tom Hale
    Jan 11, 2017 at 4:46
  • 8
    To get this to work with OSX, install findutils from homebrew, then use gfind not find. stackoverflow.com/questions/752818/…
    – Chris
    Mar 5, 2018 at 0:44
5

If that is just a depth-n (assume depth-2) folder hierarchy, I find this one useful:

ls -laht --full-time */*
4
  • 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
    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
    – Ben Usman
    Feb 13, 2017 at 17:25
  • 2
    So 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 to n with ls ... * */* */*/* ...
    – mwfearnley
    Feb 14, 2017 at 9:26
  • 1
    May not work: bash: /bin/ls: Argument list too long
    – Luc
    Jan 13, 2020 at 10:35

Not the answer you're looking for? Browse other questions tagged .