4

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.

4
  • 1
    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
    – Sirex
    Jul 17, 2015 at 2:38
  • 1
    for example: find -type f -printf '%T+\t%p\n' | sort -n
    – Sirex
    Jul 17, 2015 at 2:39
  • Sorting by creation date is impossible in Linux because it has no concept of file creation time.
    – Larssend
    Jul 17, 2015 at 2:46
  • @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
    Jul 17, 2015 at 7:43

1 Answer 1

3

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.

4
  • 1
    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
    Jul 17, 2015 at 7:43
  • What is the purpose of the {} and the \; in these commands? Jul 18, 2015 at 0:44
  • 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.
    – Larssend
    Jul 18, 2015 at 4:27
  • by time : sort -k6M -k7n
    – ychaouche
    Nov 12, 2020 at 9:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.