How to sort the output of find?

13,570

Sorting the output by creation time is impossible in Linux (ctime is not file creation date). stat has the ability to show a file's birth time using the %w and %W format tags, but they always show - and 0, respectively, even on filesystems that store creation time/birth time. Hence, it is practically useless for this purpose on Linux.

The other two sorting orders are possible, though:

# Sort by size:
find ~/ -type f -name "*inductive* -exec ls -ltu {} \; | sort -k 5 -n

# Sort by access time:
find ~/ -type f -name "*inductive* -exec ls -ltu {} \; | sort -k 6 -M

You can add the -r flag to sort to reverse the sorting order. See man sort for more information.

Depending on the size of find's output, it may take some time for sort to produce sorted output.

Share:
13,570

Related videos on Youtube

Canaryyellow
Author by

Canaryyellow

Updated on September 18, 2022

Comments

  • Canaryyellow
    Canaryyellow over 1 year

    I'm using the find command to list files with their name containing a string: find ~/ -type f -name "*inductive*" I would like to use a pipe to sort the resulting list of files. I would like to be able to sort by file size, date created, date accessed ... How can I do this? Thanks.

    • Admin
      Admin almost 9 years
      this question might help: superuser.com/questions/416308/… essentially, you put what you want to sort on as the first field by editing the output format, then sort the outputs
    • Admin
      Admin almost 9 years
      for example: find -type f -printf '%T+\t%p\n' | sort -n
    • Admin
      Admin almost 9 years
      Sorting by creation date is impossible in Linux because it has no concept of file creation time.
    • Admin
      Admin almost 9 years
      @geewee The answers to Birth is empty on ext4 offer ways around this issue with "birth time" - The field gets populated (see below) only coreutils stat does not display it. - You can get the creation time via debugfs ... I combined this into a simple shell function ...
  • DavidPostill
    DavidPostill almost 9 years
    The answers to Birth is empty on ext4 offer ways around this issue with "birth time" - The field gets populated (see below) only coreutils stat does not display it. - You can get the creation time via debugfs ... I combined this into a simple shell function ...
  • Canaryyellow
    Canaryyellow almost 9 years
    What is the purpose of the {} and the \; in these commands?
  • Larssend
    Larssend almost 9 years
    The curly braces are simply part of the syntax. See man find. Bash uses semicolon as command separator. The backslash before the semicolon treats the latter as a literal character and tells bash to pass it to find, where it's used to mark the end of -exec. It's also documented in man find.
  • ychaouche
    ychaouche over 3 years
    by time : sort -k6M -k7n