kobame
kobame

Reputation: 5856

Sort any random list of files by modification date

Looking for generic way how to sort a random list of files by its modification time, so something like:

./make_list_of_files | some_sorter_by_mtime

my currect solution is (here the make_list_of_files is the find command):

find / -type f -print |\
  perl -nle 'push @in,$_;END {@out = sort{ (stat($a))[9] <=> (stat($b))[9] } @in; $,="\n";print @out}'

exists some simpler solution (e.g. without perl)?

Upvotes: 3

Views: 296

Answers (3)

clt60
clt60

Reputation: 63922

Your some_sorter_by_mtime should be for example:

xargs stat -f "%m %N" | sort -n | cut -f2-

the idea behind is:

  • print out file modification time and the filename
  • sort the output numerically (so by modification time)
  • cut out the time field

so,

find / -type f -print | xargs stat -f "%m %N" | sort -n | cut -f2-

Upvotes: 6

ernix
ernix

Reputation: 3643

Like this?

find / -type f -print | xargs ls -l --time-style=full-iso | sort -k6 -k7 | sed 's/^.* \//\//'

Upvotes: 2

Guru
Guru

Reputation: 16994

Yes, without perl:

find / -type f -exec ls -lrt '{}' \+

Guru.

Upvotes: -2

Related Questions