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

Posted on in Categories , , , last updated March 19, 2013

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

13 comment

  1. 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.

  2. 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.

  3. 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.

    1. 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:
      [[email protected] etc]# cd /etc
      [[email protected] etc]# du -sh *|sort -n

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

  4. 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.

  5. 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 .

Leave a Comment