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.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsGnu sort
TimeN/A

ADVERTISEMENTS

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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
13 comments… add one
  • Colin Mar 20, 2013 @ 0:57

    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 Mar 20, 2013 @ 7:51

    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 Mar 20, 2013 @ 11:13

    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 Mar 20, 2013 @ 15:36

    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 Mar 20, 2013 @ 17:17

    My favorite is “ncdu”.

  • Mluther Mar 20, 2013 @ 20:16

    Alternatively one could also use: ls -lSh

    • SHARAD GALANDE Mar 21, 2013 @ 5:15

      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 Feb 26, 2014 @ 12:44

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

  • Bronson Feb 26, 2014 @ 17:31

    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 Apr 2, 2014 @ 17:05

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

  • Wellington Torrejais da Silva Jun 30, 2014 @ 11:34

    Thanks!!!

  • agentooer Jul 28, 2014 @ 4:01

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

  • dan levit Mar 31, 2016 @ 8:34

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.