how to sort du command output by size in linux

If you use a Linux system often enough, you should already be familiar with the du command. The du command allows you to check or print the file space usage. You can use it to print out the current disk usage for a set of files and folders.

The ability to print out folders sorted by size can be useful when you are trying find the files or folders that is consuming the most space on your disk. However the du command or utility does not have an option to sort by size.

One of the options you have is to pipe the output through the sort utility. The sort is a generic utility that can be used in many different scenarios to sort text or numbers. This is probably also one of the generic methods you could use to sort almost any command output in Linux.

By default the du output the values in kb or kilobytes. So, to sort the files and folder by size, you could use the following command

$ du /path | sort -n

The -n or –numeric-sort command line option specifies that the values to be sorted are integers and are to be sorted numerically. You can use the -r or –reverse option to sort it in the reverse order.

Obviously this will work as long as the size is printed out as a number, which is the case when you don't use any command line arguments to modify the size shown. It also works with the -b or –bytes option as well. But sometimes, you want to print out the size in a human readable format. You can use the -h or –human-readable option of du to do that.

$ du -hs /path

When you print out the output of du in a human readable format, the sort command will not work correctly anymore. If you use one of the latest version of sort, then you can use the -h or –human-numeric-sort option of to sort command to sort the input.

$ du -hs * | sort -h
du-sort-linux.png

The GNU coreutils package that supports this option has been released a while back in 2009 (version 7.5), so it is very likely that you already have at least this version or you can upgrade your packages. If for some reason you have an older package, then you will have to use some linux scripting to achieve the same output.

There are several different ways to do this, and most of which includes either double counting or running du command twice. None of it is really optimal or efficient. One of the easiest ways to sort with out the -h option of sort is to first use the du command with out the -h option, sort that output and then run du again on the sorted list of folders.

$ du -b * | sort -n | cut -f2-

In the above example, the du prints the size in bytes, after which it is sorted numerically. The last part uses the cut utility to print out just the folder or file name sorted according to the size. We can now pass this output again to du with the -h option to print out the human readable size.

$ du -h $(du -b * | sort -n | cut -f2-)
linux-sort-du.png

The above one liner should now print out the file and folder sizes in a human readable format sorted by the size.