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 in ls will sort by mtime.
  • xargs is used to pass the filenames as an argument to ls. Note that you have to use -print0 in find and -0 in xargs if you have files with spaces in their names. Also, the maximum amount of arguments is limited by the ARG_MAX variable. To find out how many these are, enter getconf ARG_MAX.
  • You can supply -r to reverse the sort order (→ oldest files first)
  • The other find and ls options are – as usual – outlined in the manuals (man find or man ls).