How do I get files found by command-line 'find' ordered by modification date in OS X?
Using the Mac OS X Terminal, how do I write a find
command that orders results by modification date (most recent first)?
There are similar questions already, but they refer to GNU find
and POSIX, but I do not know whether Mac OS X is GNU or POSIX or something else.
Solution 1:
OS X is fully POSIX compliant.
Something like that should do:
find . -type f -name "*.txt" -print0 | xargs -0 ls -tl
Some notes:
- The
-t
option inls
will sort bymtime
. -
xargs
is used to pass the filenames as an argument tols
. Note that you have to use-print0
infind
and-0
inxargs
if you have files with spaces in their names. Also, the maximum amount of arguments is limited by theARG_MAX
variable. To find out how many these are, entergetconf ARG_MAX
. - You can supply
-r
to reverse the sort order (→ oldest files first) - The other
find
andls
options are – as usual – outlined in the manuals (man find
orman ls
).