for example I have command that shows how much space folder takes

du folder | sort -n

it works great, however I would like to have human readable form

du -h folder

however if I do that than I cannot sort it as numeric.

How to join du folder and du -h folder to see output sorted as du folder, but with first column from du -h folder

P.S. this is just an example. this technique might be very useful for me (if its possible)


Here is a more general approach. Get the output of du folder and du -h folder in two different files.

du folder > file1
du -h folder > file2

The key part is this: concatenate file1 and file2 line by line, with a suitable delimiter.

paste -d '#' file1 file2 > file3

(assuming # does not appear in file1 and file2)

Now sort file3. Note that this will sort based on file1 contents and break ties by file2 contents. Extract the relevant result using cut:

sort -n -k1,7 file3 | cut -d '#' -f 2

Also take a look at man sort for other options.

You may also save this as an alias, for later re-use. To do so, add the following to the bottom of ~/.bashrc:

sorted-du () {
    paste -d '#' <( du "$1" ) <( du -h "$1" ) | sort -n -k1,7 | cut -d '#' -f 2

Then, open a new terminal session and execute your new alias:

sorted-du /home
  • I added the last part on how to make your solution into an alias.
    – SirCharlo
    Aug 30 '12 at 17:50
  • 1
    Using actual files in an alias (and then calling rm on them) is a really really bad idea! Think about what would happen if I actually had a file named file3 in my directory, it would be overwritten and then deleted! @SirCharlo I am removing the alias definition from this answer and replacing it with something safer.
    – jmiserez
    Dec 31 '13 at 11:10
  • Well askubuntu.com/a/80248/22308 simply do this with sort -h
    – Nam G VU
    Jun 15 '19 at 3:28

Try something like:

du -h folder | sort -h

Alternatives: -n for Numerical sorting

Note: the -h option of sort only exists in newer versions of Ubuntu.

  • 3
    there is in version 8.17, so I guess this is the easiest way
    – steabert
    Aug 30 '12 at 18:59
  • 1
    Thanks to steabert for pointing out that the sort command has been improved from sort --version = 8.17 This is the best answer for newer versions of Ubuntu. Aug 30 '12 at 19:07
  • This should be the accepted one cause it clean-shoot that resolve the OP with sort -h
    – Nam G VU
    Jun 15 '19 at 3:27

This answer is valid for 10.04.4LTS and lower versions of Ubuntu.

Unfortunatly the accurate answer which sorts K M G is difficult and complex:

You can alias the entire du command with one that sorts human readable using this

alias duf='du -sk * | sort -n | perl -ne '\''($s,$f)=split(m{\t});for (qw(K M G)) {if($s<1024) {printf("%.1f",$s);print "$_\t$f"; last};$s=$s/1024}'\'

which I found here


just cd into the folder you would like to know then duf

you could add this duf alias to the end of your /home/user/.profile to make the duf command semi-permenant


user@hostname:~$ duf
0.0K  Documenten
0.0K  Muziek
0.0K  Openbaar
0.0K  Sjablonen
0.0K  Video's
4.0K  backup_db.sql.g
4.0K  examples.desktop
12.0K xml printer ticket
52.0K hardinfo_report.html
152.0K    librxtxSerial.so
2.7M  jpos
4.4M  nxclient_3.5.0-7_amd64.deb
6.4M  nxnode_3.5.0-4_amd64.deb
7.4M  nxserver_3.5.0-5_amd64.deb
12.4M NetBeansProjects
18.1M mysqlworkbench.deb
28.3M Afbeeldingen
45.8M ergens-20110928-18.sql.gz
60.5M 2012-06-02ergens_archive.tar.gz
65.5M 2012-08-26ergens_archive.tar.gz
65.6M 2012-08-28ergens_archive.tar.gz
65.6M 2012-08-29ergens_archive.tar.gz
65.7M 2012-08-30ergens_archive.tar.gz
113.0M    Bureaublad
306.2M    ergens-20110928-18.sql

Here is why du -sch /var/* | sort -n does not work see the sorting of MKKMMKKMMK

user@hostname:~$ du -sch /var/* |sort -n

0 /var/crash
0 /var/local
0 /var/lock
0 /var/opt
8,0M  /var/backups
12K   /var/games
16K   /var/tmp
17M   /var/log
68M   /var/cache
104K  /var/spool
144K  /var/run
351M  /var/lib
443M  totaal
704K  /var/mail
  • I've reported this to the gnu-core developers with a feature request to improve the du -h function with a sort feature. via bugs-coreutils@gnu.org Aug 30 '12 at 18:40
  • +1, similar one-liner: du folder | sort -n | cut -f 2 | while IFS= read -r -d '' path; do du -sh -- "$path"; done
    – steabert
    Aug 30 '12 at 18:55
  • Just tried your 1 liner on a live system. Get a Access Denied error and no du results. Did you try this command on an Ubuntu system first? I changed folder to /home/username Aug 30 '12 at 19:11
  • oh, sorry, you should drop the -d '', that's a left-over from using it on the output of the file command.
    – steabert
    Aug 30 '12 at 19:43
  • This seems to work great on non-Linux systems which don't support GNU sort. Nov 20 '17 at 22:08

Command GNU sort has the following option:

-h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G).

To have this option on BSD/OSX, you can install sort from coreutils (via brew) and add the bin folder to your PATH into your rc files.

So the command would looks like:

du -ah . | sort -rh | head -20

For recent versions of Ubuntu, use du -h directory | sort -h .

I use a form of this all the time for finding out-of-control files.

  • Duplicate of answered Nov 17 '11 at 17:13 Allu2
    – Elder Geek
    Mar 29 '15 at 16:40

Open your terminal with Ctrl+Alt+t and type:

  du -sk * | sort -rn 
  • :D I am now used to changing the Ctrl+Alt+t to the format above so much so that I have pasted the required format to my Xpad. :D Feels great to be complimented for such a menial job :)
    – jobin
    Dec 23 '13 at 15:47
  • Every work you did here worth complement.
    – rɑːdʒɑ
    Dec 23 '13 at 16:27

This one handles filenames with whitespace or apostrophes, and works on systems which do not support xargs -d or sort -h:

du -s * | sort -n | cut -f2 | tr '\n' '\0' | xargs -0 -I {} du -sh "{}"

which results in:

368K    diskmanagementd
392K    racoon
468K    coreaudiod
472K    securityd
660K    sshd
3.6M    php-fpm
  • What's the point of replacing \n with \0? Isn't it a bit too late for that?
    – muru
    Nov 23 '14 at 19:45
  • it prepares the input for xargs -0, which expects the input to be NUL delimited (as find -print0 would produce). Otherwise you can’t use -0, and apostrophes have special meaning. Nov 23 '14 at 19:51
  • I know about -0, but it's irrelevant: imgur.com/87w3vfj
    – muru
    Nov 23 '14 at 20:02
  • I think that is working as you have used a backtick, not an apostrophe. gist.github.com/mrmanc/8aef830945ce03aa65f0 Nov 24 '14 at 15:17

The one I found working on all systems regardless of the version (sort -h doesn't work for me) is

du -h $(du * | sort -n | awk '{print $2}')

the awk '{print $2}' basically prints file names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.