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.
-
1this 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– SirexJul 17, 2015 at 2:38
-
1for example: find -type f -printf '%T+\t%p\n' | sort -n– SirexJul 17, 2015 at 2:39
-
Sorting by creation date is impossible in Linux because it has no concept of file creation time.– LarssendJul 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
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.
-
1The 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
-
-
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 tofind
, where it's used to mark the end of-exec
. It's also documented inman find
.– LarssendJul 18, 2015 at 4:27 -