1

I want to use the GNU find command to find files based on a pattern, and then have them displayed in order of the most recently modified file to the least recently modified.

I understand this:

find / -type f -name '*.md'

but then what would be added to sort the files from the most recently modified to the least?


devLinuxNC
  • 81
  • 1
  • 8

3 Answers3

2

find can't sort files, so you can instead output the modification time plus filename, sort on modification time, then remove the modification time again:

find . -type f -name '*.md' -printf '%T@ %p\0' |   # Print time+name
  sort -rnz |                                      # Sort numerically, descending
  cut -z -d ' ' -f 2- |                            # Remove time
  tr '\0' '\n'                                     # Optional: make human readable

This uses \0-separated entries to avoid problems with any kind of filenames. You can pass this directly and safely to a number of tools, but here it instead pipes to tr to show the file list as individual lines.

that other guy
  • 116,971
  • 11
  • 170
  • 194
1
find <dir> -name "*.mz" -printf "%Ts - %h/%f\n" | sort -rn

Print the modified time in epoch format (%Ts) as well as the directories (%h) and file name (%f). Pipe this through to sort -rn to sort in reversed number order.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
0

Pipe the output of find to xargs and ls:

find / -type f -name '*.md' | xargs ls -1t
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 1
    I'd recommend `find ... -exec ls 1t {} +` over `xargs`. `-exec +` is not only faster, but also avoids a lot of quoting issues. – Socowi Dec 11 '20 at 22:30