≡ 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
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 13 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.

  • dan levit March 31, 2016, 8:34 am

    Hi every one ,i’m new in Unix and i try to write the following script in csh
    i want to find all the dirctorys that start with the same name lets say “Jonson”
    and the output should be a sorted list :
    name size date
    in this order( the date means the last time the current directory was modified) .
    and the output should be sorted by the size of the the directory from small to big ..

    I’m pretty lost ..

    thanks .

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , ,