≡ Menu

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

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.

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

{ 12 comments… add one }

  • Colin March 20, 2013, 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.

  • Llama March 20, 2013, 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.

  • cap_protect March 20, 2013, 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.

  • bkaraff March 20, 2013, 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.

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

    My favorite is “ncdu”.

  • Mluther March 20, 2013, 8:16 pm

    Alternatively one could also use: ls -lSh

    • SHARAD GALANDE March 21, 2013, 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

  • David February 26, 2014, 12:44 pm

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

  • Bronson February 26, 2014, 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.

  • esvom April 2, 2014, 5:05 pm

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

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


  • agentooer July 28, 2014, 4:01 am

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

Leave a Comment

   Tagged with: , , , , , ,