How to sort the output of find?
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.
Related videos on Youtube
Canaryyellow
Updated on September 18, 2022Comments
-
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 almost 9 yearsthis 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 almost 9 yearsfor example: find -type f -printf '%T+\t%p\n' | sort -n
-
Admin almost 9 yearsSorting by creation date is impossible in Linux because it has no concept of file creation time.
-
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 almost 9 yearsThe 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 almost 9 yearsWhat is the purpose of the {} and the \; in these commands?
-
Larssend almost 9 yearsThe 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 tofind
, where it's used to mark the end of-exec
. It's also documented inman find
. -
ychaouche over 3 yearsby time :
sort -k6M -k7n