Do you want to use du
to sort by size, date or other properties?
du
is a low-level Linux/Unix program which allows an user to get a summary of how much disk usage has been used by files and directories. Like any other standard Linux programs, the user can take advantage of many options or flags with du
to modify the program output. du
can also display file sizes and directories in a recursive manner.
This article will show you a few ways to sort files and directories by size, date and other properties using du
command combined with sort
– another standard program in Linux.
du – sort by size
By default, du
output isn’t sorted and there is no flags to do that either. To be able to sort du
output, we have to pipe it to another program : sort
.
What sort
does is that it takes lines of text and sort them based on flags and switches passed on to it. Because du
outputs the size in the beginning of the line, we can leverage --numeric-sort
option to have sort
done the job.
The following command sorts du
output by size in descending order.
du /path/to/directory | sort -n -r
The -n
flag is a shorthand of --numeric-sort
, which tells sort
to sort lines in numerical order. The -r
flag means -reverse
, which reverse the default ascending order of sort
.

If you want du
output to be sorted in ascending order, just remove -r
so the command would look like this:
du /path/to/directory | sort -n

By default, du
scans all subdirectories and includes them in the output. If you want only the direct children of the path to be scanned, put -d
or --max-depth
into du
command like below.
du /path/to/directory -d 1 | sort -n
OR
du /path/to/directory --max-depth=1 | sort -n
-d
switch allows you to specify how deep you want du
program to scan. -d=1
means only the direct children of /path/to/directory
are include in the output.

du – sort by size and get top 10 biggest
Let’s suppose we want to sort du
output by size in descending order, then get only 10 files and directories that eats up the largest amount of space. In order to do that, head
is another standard Linux program that comes in handy.
du /path/to/directory | sort -n -r | head
The command above pipe du
output to sort
, which will have the result sorted in reversed order (descending). After that, the output is then piped into head
, which trim it to the first 10 lines by default.

du – sort by size and get top 10 smallest
If you want to get a list of 10 smallest files and directories, you can use a similar command like the one from above example. The only difference is that we now drop -r
flag to sort the output in ascending order.
du /path/to/directory | sort -n | head

du – sort by size in human-readable numbers
If you want your file size to be human-readable instead of just numbers, du
has -h
switch for that purpose. You just have to put -h
into du
command before piping it over to sort
.
du /path/to/directory -h | sort -n -r
Or, combine the flags for faster typing :
du /path/to/directory -h | sort -nr
The results should look like the image below

As of GNU coreutils 7.5 released in August 2009, sort allows a -h
parameter, short for --human-numeric-sort
, which allows numeric suffixes of the kind produced by du -h
du -hs * | sort -h
Sort files by time and date
du
is made to estimate file space usage and nothing else. To be able to sort files by time/date, we need to use ls
, which lists the files in a path along with information about them.
ls /path/to/directory -lht
-l
tells ls
to list the directory in long listing format, which makes it easier to read.
-h
is short for --human-readable
, which prints file and directory sizes in human-readable form (like 1G or 100M).
-t
is the most important part of the command, the output will be sorted by time if you use this flag.
https://serverfault.com/questions/62411/how-can-i-sort-du-h-output-by-size
sort has a h flag that puts megabytes before kilobytes.
I was starting awk’ing and doing crazy stuff and simple `sort -h` is doing it with no hastle. Thanks for mentioning this flag in a comment. Probably it should be included in the article.