I need to sort the results of 'du' command by file or folder name, file size, or last modified time in various situations, and appending ' | sort -k 2' command after 'du' command sorts the results by folder names, which I learned from this question. Sort by filename with du command

In the sort command manual (RHEL 7.6), it reads -k, --key=KEYDEF sort via a key; KEYDEF gives location and type KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field;

but still don't understand what it means.

Does F=2 means file name? Then what is the F for the file size or last modified time? Then where can I find the 'KEYDEF'?

I carefully read this document: https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html but still can't find the information.

1 Answer 1


F is the field number, and by default, the sort command defines a field as text surrounded by white space. The -b and -t options can change the default; the document you reference contains a lengthy headache-inducing discussion about the details of specifying field separators depending on locale and such.

You ask "what is the F for the file size or last modified time?". sort doesn't have the notion of file size or modified time. Since your goal is to filter the output of du, run your du command and count the fields.

For example, when I run du without option, I get two fields per line, the size and the file/directory name:

$ du . 
8       ./ruby
4       ./nextcloud/data/nextcloud
12      ./nextcloud/data/database/nextcloud
4       ./nextcloud/data/database/performance_schema
4       ./nextcloud/data/database/mysql
... etc ...

In case you need to sort files and directories under the current directory, du . | sort -n does the trick, because sort sorts on the first field by default. The -n option is required to sort numerically rather than alphanumerically:

$ du . | sort -n 
4       ./bitwarden/data/icon_cache
4       ./.cache/obexd
4       ./.cache/pip/wheels/2c/4a/61/5599631c1554768c6290b08c02c72d7317910374ca602ff1e5
4       ./.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
4       ./.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
4       ./.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
... etc ...

You can add -r to display big files first.

To sort by pathname:

du . | sort -k 2

I don't have a quick solution for sorting by filename, i.e. by the last component of the pathname. I think sort alone can't to that, because you would need to figure out which subfield is the filename, and the subfield number varies. Here is a partial solution (it only works for pathnames that contain a slash):

$ du . | sed 's|\(.*\)/\(.*\)|\1/\2 \2|' | sort -k3
5639116 .
12      ./.cache/pip/http/9/8/7/0 0
12      ./.cache/pip/http/9/c/b/c/0 0
12      ./.cache/pip/http/c/0/4/0/0 0
12      ./.cache/pip/http/f/0/3/0 0
12      ./.cache/pip/http/f/e/d/0 0
136     ./.cache/pip/http/4/8/d/7/0 0
164     ./.cache/pip/http/a/0 0
16      ./.cache/pip/http/0/0/9/7/0 0
16      ./.cache/pip/http/2/8/0 0
... etc ...

I don't know if there is any combinations of options that makes the du command print the modified time. If there is one, let me know, and I will point out which field it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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