This question already has an answer here:

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.

marked as duplicate by Sirex, mdpc, Steven, LPChip, Dave Jul 23 '15 at 10:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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.

  • 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 '15 at 7:43
  • What is the purpose of the {} and the \; in these commands? – Canaryyellow Jul 18 '15 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 '15 at 4:27
  • Now I see. Thanks, I'm new to this. – Canaryyellow Jul 23 '15 at 20:47

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