Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to get a list of human readable du output.

However, du does not have a "sort by size" option, and piping to sort doesn't work with the human readable flag.

For example, running:

du | sort -n -r 

Outputs a sorted disk usage by size (descending):

du |sort -n -r
65108   .
61508   ./dir3
2056    ./dir4
1032    ./dir1
508     ./dir2

However, running it with the human readable flag, does not sort properly:

du -h | sort -n -r

508K    ./dir2
64M     .
61M     ./dir3
2.1M    ./dir4
1.1M    ./dir1

Does anyone know of a way to sort du -h by size?

share|improve this question

migrated from Sep 4 '09 at 13:06

This question came from our site for professional and enthusiast programmers.

Heh ... Funny you should ask, as this has been annoying me for ... well over a year at least. Last week I downloaded the code to GNU coreutils (of which sort is a part), and had a look, but decided it would take a bit more time than I had on my hands to patch ... Anyone? :) – unwind Feb 25 '09 at 13:46
Here's a much related question: – cregox Nov 19 '15 at 14:15

34 Answers 34

up vote 568 down vote accepted

As of GNU coreutils 7.5 released in August 2009, sort allows a -h parameter, which allows numeric suffixes of the kind produced by du -h:

du -hs * | sort -h

For Mac, you can use this(from comments):

brew install coreutils
du -hs * | gsort -h
share|improve this answer
The relevant section of the manual:… – wodow Feb 9 '11 at 11:13
this is clearly the right answer on modern systems. Thanks! – simon Mar 3 '11 at 3:36
Easy to install on OS X with homebrew -- brew install coreutils. – Richard Poirier May 1 '11 at 18:53
Good one! I personally always did du -BM | sort -nr as a workaround - it is human readable enough, and it is sorted, if anyone is stuck with older coreutils. – chutz May 24 '12 at 9:06
If using on OSX via Homebrew, note that you'll now need to use gsort rather than sort: du -hs * | gsort -h – Brian Cline Dec 16 '13 at 8:45
du | sort -nr | cut -f2- | xargs du -hs
share|improve this answer
And it'll do a huge amount of duplicate counting. – Douglas Leeder Feb 25 '09 at 13:55
it would help if you explain why that works?? – hasen Feb 25 '09 at 14:10
@Douglas Leeder: you are right for the duplicate counting, but think that the second du does not start from cold cache (thanks to the OS) @hasen j: xargs is a very useful command, it splits its stdin and feeds it as arguments to the given command – cadrian Feb 25 '09 at 14:52
Chris's is actually superior since it works with paths containing whitespace. Throwing a vote your way, buddy. – rbright Feb 25 '09 at 22:45
Ugly, but cross-platform :). – voretaq7 Nov 29 '11 at 23:06

There is an immensely useful tool I use called ncdu that is designed for finding those pesky high disk-usage folders and files, and removing them. It's console based, fast and light, and has packages on all the major distributions.

share|improve this answer
Very nice... I wondier if the results could be fed to standard out... I am so lazy that I cannot read the manual – ojblass Jun 27 '09 at 5:17
gt5 is in the same vein; its killer feature is displaying growth. – Tobu Jul 1 '10 at 16:16
That's really cool! And much faster than hanging around with du, if you just want to identify the large directories. – BurninLeo Sep 16 '15 at 17:49

@Douglas Leeder, one more answer: Sort the human-readable output from du -h using another tool. Like Perl!

du -h | perl -e 'sub h{%h=(K=>10,M=>20,G=>30);($n,$u)=shift=~/([0-9.]+)(\D)/;
return $n*2**$h{$u}}print sort{h($b)<=>h($a)}<>;'

Split onto two lines to fit the display. You can use it this way or make it a one-liner, it'll work either way.


4.5M    .
3.7M    ./colors
372K    ./plugin
128K    ./autoload
100K    ./doc
100K    ./syntax

EDIT: After a few rounds of golf over at PerlMonks, the final result is the following:

perl -e'%h=map{/.\s/;99**(ord$&&7)-$`,$_}`du -h`;die@h{sort%h}'
share|improve this answer
Your short version outputs on stderr because of the die can you change it to make it output on stdout? – Dennis Williamson Sep 4 '09 at 16:16
Change the die to a print and it will go to stdout. It's just two more characters. – Adam Bellaire Sep 9 '09 at 17:55
works on ubuntu! – marinara Apr 26 '12 at 8:22
impressive perl hackistry – nandoP Nov 13 '13 at 5:00
The result is in reverse order :( – RSFalcon7 Apr 1 '14 at 22:28
du -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh
share|improve this answer
just what I was looking for thanks – Edward Tanguay Jun 6 '09 at 9:39
Can't use with du -k --total, gives error at the end du: cannot access 'total': No such file or directory – laggingreflex Jan 3 '15 at 2:51
i like this one more any other answer. how would you go to show only the first 50 results? – Mauro Jan 12 at 4:25

As far as I can see you have three options:

  1. Alter du to sort before display.
  2. Alter sort to support human sizes for numerical sort.
  3. Post process the output from sort to change the basic output to human readable.

You could also do du -k and live with sizes in KiB.

For option 3 you could use the following script:

#!/usr/bin/env python

import sys
import re

sizeRe = re.compile(r"^(\d+)(.*)$")

for line in sys.stdin.readlines():
    mo = sizeRe.match(line)
    if mo:
        size = int(
        if size < 1024:
            size = str(size)+"K"
        elif size < 1024 ** 2:
            size = str(size/1024)+"M"
            size = str(size/(1024 ** 2))+"G"

        print "%s%s"%(size,
        print line
share|improve this answer

I've had that problem as well and I'm currently using a workaround:

du -scBM | sort -n

This will not produce scaled values, but always produce the size in megabytes. That's less then perfect, but for me it's better than nothing (or displaying the size in bytes).

share|improve this answer
I like th -BM switch, which is basically the same as -m, but it has the advantage of displaying the size and M postfixed to it, so you get 10M which is much clearer than just 10 :) – Tom Feiner Feb 25 '09 at 14:02
This is the simplest solution I've seen so far on this page, thank you! – Jeff Olson Nov 5 '15 at 16:35

Found this posting elsewhere. Therefore, this shell script will do what you want without calling du on everything twice. It uses awk to convert the raw bytes to a human-readable format. Of course, the formatting is slightly different (everything is printed to one decimal place precision).

du -B1 | sort -nr  |awk '{sum=$1;
for (x=1024**3; x>=1024; x/=1024){
        if (sum>=x) { printf "%.1f%s\t\t",sum/x,hum[x];print $2;break

Running this in my .vim directory yields:

4.4M            .
3.6M            ./colors
372.0K          ./plugin
128.0K          ./autoload
100.0K          ./syntax
100.0K          ./doc

(I hope 3.6M of color schemes isn't excessive.)

share|improve this answer
I have a Perl answer, too, but I think it might make people hate me: du -B1 | sort -nr | perl -e '%h=(0=>b,1=>K,2=>M,3=>G);for(<>){($s,@f)=split/\s+/;$e=3;$e-- while(1024**$e>$s);$v=($s/(1024**$e));printf "%-8s%s\n",sprintf($v >= 100 ? "%d%s" : "%.1f%s",$s/(1024**$e),$h{$e}),@f;}' – Adam Bellaire Feb 25 '09 at 14:40
Even though the Perl answer actually gives its formatting much closer to du. Although the rounding is off... It looks like du always gives ceil() rather than round() – Adam Bellaire Feb 25 '09 at 14:41
Hey, why did I use a hash there? Should've been an array... morning-brain grumble.... – Adam Bellaire Feb 25 '09 at 15:33
Added a better Perl solution as another answer. – Adam Bellaire Feb 25 '09 at 21:06
Both versions fail when filenames contain spaces – Vi. Nov 2 '10 at 17:54

Here's an example that shows the directories in a more compact summarized form. It handles spaces in directory/filenames.

% du -s * | sort -rn | cut -f2- | xargs -d "\n" du -sh

53G  projects
21G  Desktop
7.2G VirtualBox VMs
3.7G db
3.3G SparkleShare
2.2G Dropbox
272M apps
47M  incoming
14M  bin
5.7M rpmbuild
68K  vimdir.tgz
share|improve this answer

This version uses awk to create extra columns for sort keys. It only calls du once. The output should look exactly like du.

I've split it into multiple lines, but it can be recombined into a one-liner.

du -h |
  awk '{printf "%s %08.2f\t%s\n", 
    index("KMG", substr($1, length($1))),
    substr($1, 0, length($1)-1), $0}' |
  sort -r | cut -f2,3


  • BEGIN - create a string to index to substitute 1, 2, 3 for K, M, G for grouping by units, if there's no unit (the size is less than 1K), then there's no match and a zero is returned (perfect!)
  • print the new fields - unit, value (to make the alpha-sort work properly it's zero-padded, fixed-length) and original line
  • index the last character of the size field
  • pull out the numeric portion of the size
  • sort the results, discard the extra columns

Try it without the cut command to see what it's doing.

Here's a version which does the sorting within the AWK script and doesn't need cut:

du -h |
   awk '{idx = sprintf("%s %08.2f %s", 
         index("KMG", substr($1, length($1))),
         substr($1, 0, length($1)-1), $0);
         lines[idx] = $0}
    END {c = asorti(lines, sorted);
         for (i = c; i >= 1; i--)
           print lines[sorted[i]]}'
share|improve this answer
thank you! this is the first example that works for me in OS X 10.6 not counting the perl/phython-scripts. and thanks again for the good explanation. always nice to learn something new. awk sure is a powerful tool. – Wolf May 4 '11 at 12:09

I've a simple but useful python wrapper for du called dutop. Note that we (the coreutils maintainers) are considering adding the functionality to sort to sort "human" output directly.

share|improve this answer
+1 for one of the rare, valid exceptions to "do one thing and do it right". Unless someone gets sort to understand the SI-prefix and/or the binary prefixes. – Joachim Sauer Mar 18 '09 at 22:20
And as ptman mentions below: ta da! (new sort flag) – Tobu Jul 1 '10 at 16:13

Got another one:

$ du -B1 | sort -nr | perl -MNumber::Bytes::Human=format_bytes -F'\t' -lane 'print format_bytes($F[0])."\t".$F[1]'

I'm starting to like perl. You might have to do a

$ cpan Number::Bytes::Human

first. To all the perl hackers out there: Yes, I know that the sort part can also be done in perl. Probably the du part, too.

share|improve this answer

Use the "-g" flag

 -g, --general-numeric-sort
              compare according to general numerical value

And on my /usr/local directory produces output like this:

$ du |sort -g

0   ./lib/site_ruby/1.8/rubygems/digest
20  ./lib/site_ruby/1.8/rubygems/ext
20  ./share/xml
24  ./lib/perl
24  ./share/sgml
44  ./lib/site_ruby/1.8/rubygems/package
44  ./share/mime
52  ./share/icons/hicolor
56  ./share/icons
112 ./share/perl/5.10.0/YAML
132 ./lib/site_ruby/1.8/rubygems/commands
132 ./share/man/man3
136 ./share/man
156 ./share/perl/5.10.0
160 ./share/perl
488 ./share
560 ./lib/site_ruby/1.8/rubygems
604 ./lib/site_ruby/1.8
608 ./lib/site_ruby
share|improve this answer
That doesn't give the human-readable output, though, which is what the OP was looking for. – Jenn D. Feb 25 '09 at 17:24

This snippet was shameless snagged from 'Jean-Pierre' from Is there a way I can better credit him?

du -k | sort -nr | awk '
     BEGIN {
        split("KB,MB,GB,TB", Units, ",");
        u = 1;
        while ($1 >= 1024) {
           $1 = $1 / 1024;
           u += 1
        $1 = sprintf("%.1f %s", $1, Units[u]);
        print $0;
share|improve this answer
i think if it is a very big number, then the unit is gone and the number displayed is small... try 23423423432423 – 太極者無極而生 Apr 22 '15 at 9:18

sort files by size in MB

du --block-size=MiB --max-depth=1 path | sort -n
share|improve this answer

Another one:

du -h | perl -e'
@l{ K, M, G } = ( 1 .. 3 );
print sort {
    ($aa) = $a =~ /(\w)\s+/;
    ($bb) = $b =~ /(\w)\s+/;
    $l{$aa} <=> $l{$bb} || $a <=> $b
  } <>'
share|improve this answer

Found this one on line... seems to work OK

du -sh * | tee /tmp/duout.txt | grep G | sort -rn ; cat /tmp/duout.txt | grep M | sort -rn ; cat /tmp/duout.txt | grep K | sort -rn ; rm /tmp/duout.txt

share|improve this answer

I learned awk from concocting this example yesterday. It took some time, but it was great fun, and I learned how to use awk.

It runs only du once, and it has a output much similar to du -h

du --max-depth=0 -k * | sort -nr | awk '{ if($1>=1024*1024) {size=$1/1024/1024; unit="G"} else if($1>=1024) {size=$1/1024; unit="M"} else {size=$1; unit="K"}; if(size<10) format="%.1f%s"; else format="%.0f%s"; res=sprintf(format,size,unit); printf "%-8s %s\n",res,$2 }'

It shows numbers below 10 with one decimal point.

share|improve this answer

Here is the simple method I use, very low resource usage and gets you what you need:

du --max-depth=1 | sort -n | awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}'

0 MB ./etc
1 MB ./mail
2 MB ./tmp
123 MB ./public_html
share|improve this answer

du -cka --max-depth=1 /var/log | sort -rn | head -10 | awk '{print ($1)/1024,"MB ", $2'}

share|improve this answer


du -sk /var/log/* | sort -rn | awk '{print $2}' | xargs -ia du -hs "a"
share|improve this answer

Another awk solution -

du -k ./* | sort -nr | 
awk '
{x = 1;while ($1 >= 1024) 
{$1 = $1 / 1024;x = x + 1} $1 = sprintf("%-4.2f%s", $1, size[x]); print $0;}'

[jaypal~/Desktop/Reference]$ du -k ./* | sort -nr | awk '{split("KB,MB,GB",size,",");}{x = 1;while ($1 >= 1024) {$1 = $1 / 1024;x = x + 1} $1 = sprintf("%-4.2f%s", $1, size[x]); print $0;}'
15.92MB ./Personal
13.82MB ./Personal/Docs
2.35MB ./Work Docs
1.59MB ./Work Docs/Work
1.46MB ./Personal/Raa
584.00KB ./scan 1.pdf
544.00KB ./Personal/Resume
44.00KB ./Membership.xlsx
16.00KB ./Membership Transmittal Template.xlsx
share|improve this answer

I had been using the solution provided by @ptman, but a recent server change made it no longer viable. Instead, I'm using the following bash script:

# File:
# list contents of the current directory by increasing 
#+size in human readable format

# for some, "-d 1" will be "--maxdepth=1"
du -k -d 1 | sort -g | awk '
    printf("%.0f KB\t%s",$1,$2);
else if($1<1024*1024)
    printf("%.1f MB\t%s",$1/1024,$2);
    printf("%.1f GB\t%s",$1/1024/1024,$2);
share|improve this answer

At least with the usual tools, this will be hard because of the format the human-readable numbers are in (note that sort does a "good job" here as it sorts the numbers - 508, 64, 61, 2, 2 - it just can't sort floating point numbers with an additional multiplier).

I'd try it the other way round - use the output from "du | sort -n -r" and afterwards convert the numbers to human-readable format with some script or program.

share|improve this answer

What you can try is:

for i in `du -s * | sort -n | cut -f2`
  du -h $i;

Hope that helps.

share|improve this answer
that's what xargs does ;-) – cadrian Feb 25 '09 at 14:05
hehe, I always forget about xargs. ;) At the end of the day, whatever gets the job done imo. – Christian Witts Feb 25 '09 at 15:05
MacOSX by default (i.e. outside of home-brew) doesn't support a proper xargs so this form was necessary. However for files with spaces in them you need to set IFS: IFS=$'\n' – HankCa Jan 30 at 11:16
du | sort -nr | awk '{ cmd = "du -h -d0 "$2"| cut -f1"; cmd | getline human; close(cmd); print human"\t"$2 }'
share|improve this answer

The following solution is similar to cadrian's original however this will only run 2 du commands as opposed to one du for each directory in the tree.

du -hs `du |sort -g |cut -f2- `

However Cardrian's solution is more robust as the above will not work for very heavily populated trees as it could exceed the limit on the size of the arguments passed to du

share|improve this answer

If you need to handle spaces you can use the following

 du -d 1| sort -nr | cut -f2 | sed 's/ /\\ /g' | xargs du -sh

The additional sed statement will help alleviate issues with folders with names such as Application Support

share|improve this answer

Instead of raping du and friends, you can use ls alone to do what you want:

ls -1Ssh

That will print all files sorted by size written in human-readable form. The first line it prints is the total, if you want to get rid of it you can simply use

ls -1Ssh | tail -n +2

You can add the -r flag to ls if you want the files in the reversed order (from smallest to largest).

share|improve this answer
At least here, that doesn't work. houdini@clanspum:~/ > ls -1Ssh | grep clanspum 4.0K drwxr-xr-x 27 houdini users 4.0K 2010-02-15 12:35 clanspum/ 0 drwxr-xr-x 2 houdini users 77 2010-02-15 13:06 clanspum-s/ houdini@clanspum:~/ > du -sh clanspum/ 602M clanspum/ – Bill Weiss Feb 23 '10 at 20:40
... pretend that was formatted correctly. Point is, it shows a directory as being "4.0K", while du -sh shows "602M". The latter is correct. – Bill Weiss Feb 23 '10 at 20:40

Here's my solution, a simple bash script that only calls du once, and shows you only directories of size 1 MB or larger:

#!/bin/env bash
# Usage: [subdirectory levels]
#   For efficiency, only calls "du" once, and stores results in a temp file
#   Stephen Becker, 2/23/2010

if [ $# -gt 0 ]; then
# You may prefer, as I do, to just summarize the contents of a directory
# and not view the size of its subdirectories, so use this:
    du -h --max-depth $1 > temp_du_file
    du -h > temp_du_file

# Show all directories of size > 1 GB:
cat temp_du_file | grep "^\([0-9]\|\.\)\+G" | sort -nr
# Show all directories of size > 1 MB:
cat temp_du_file | grep "^\([0-9]\|\.\)\+M" | sort -nr

rm temp_du_file
share|improve this answer

protected by Mark Henderson Mar 11 '13 at 11:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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