Linux: Sort du -h (human-readable) Output By Size

by on March 19, 2013 · 12 comments· LAST UPDATED March 19, 2013

in , , ,

I have a large number of files stored in ~/Downloads/ directory. How do I sort and print sizes in human readable format using du -h command under Ubuntu Linux LTS version 12.04 or any other Linux distributions?

You can pass the -h or --human-numeric-sort option to the sort command to sort and compare human readable numbers such as 2K, 300M, 1G and more. This is a new option added the gnu/sort command.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsGnu sort
Estimated completion timeN/A

sort syntax

The syntax is:
command | sort -h

To sort du command output in human readable format by size, enter:

 
du -h | sort -h
du --human-readable | sort --human-numeric-sort
 

Sample outputs:

Fig.01: du | sort in action

Fig.01: du | sort in action


To reverse the result of comparisons pass the -r option:

 
du -h | sort -h -r
du --human-readable | sort --human-numeric-sort -r
 

To see top 10 files pass the output to the head command, enter:

 
du -h | sort -h | head
du -h | sort -hr | head
du --human-readable | sort --human-numeric-sort | head
du --human-readable | sort --human-numeric-sort -r | head
 
See also
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 12 comments… read them below or add one }

1 Colin March 20, 2013 at 12:57 am

That is so cool. I used to just do du -h | grep G or M | sort

seems to work in latest centos. I cannot see the screenshot under output but just running du -h | sort -h is good enough.

Reply

2 Llama March 20, 2013 at 7:51 am

my favorite is “du -chad 1 | sort -h”. this way it only shows the files and folders in the current directory, instead of listing all files in sub-folders. Less screen spam.

Reply

3 cap_protect March 20, 2013 at 11:13 am

I have special tool https://github.com/captain-protect/diskbiggest for the cases when I need to find what is wasting a disk space. It is the same as “du -h | sort -h -r | head -n 30″, but it is multi-platform, shows progress, use less memory and display results on Ctrl-C.

Reply

4 bkaraff March 20, 2013 at 3:36 pm

Making it human-readable is cool, but if you’re in a less-than-homogenous environment with some legacy unix like I am, it can be more useful to find the common switches.

Personally, I use ‘du -xam /var/ |sort -n’ about a hundred times a day.

Reply

5 Sid Burn March 20, 2013 at 5:17 pm

My favorite is “ncdu”.

Reply

6 Mluther March 20, 2013 at 8:16 pm

Alternatively one could also use: ls -lSh

Reply

7 SHARAD GALANDE March 21, 2013 at 5:15 am

We can also use below command to sort and print sizes in human readable format using du -sh command under Solaris, Ubuntu Linux and all RHEL versions
In below example first I enter to /etc directory and then used second command to fullfill our requirement:
[root@san etc]# cd /etc
[root@san etc]# du -sh *|sort -n

Use -r option with sort to sort the output recursively :
[root@san etc]# du -sh *|sort -r -n

Reply

8 David February 26, 2014 at 12:44 pm

sort -h does’t work on centos 5.
I’m trying to find an alternative.

Reply

9 Bronson February 26, 2014 at 5:31 pm

Because I used to have to support AIX and RHEL4, I got into the habit of using:

‘du -xam |sort -n’

That way it was OS-neutral and still works everywhere.
I use this several times a day on /var/log/ for example to find what’s filling up the filesystem.

Reply

10 esvom April 2, 2014 at 5:05 pm

sort -h doesn’t work in SuSE 11. :'(

Reply

11 Wellington Torrejais da Silva June 30, 2014 at 11:34 am

Thanks!!!

Reply

12 agentooer July 28, 2014 at 4:01 am

It seems `sort -h` doesn’t work with sort 8.4.
it works with sort 8.21.

Reply

Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: