Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

command: ncdu

Directory navigation, sorting (name and size), graphing, human readable, etc...

share|improve this answer
Great utility, but not installed by default on any OS I'm aware of. Not necessarily a problem, but one more program to have to look after... – voretaq7 Nov 29 '11 at 23:07

Why not throw another hat into the ring.... it's an old question, but here's an example that is (mostly) pure shell script (fwiw) -- i.e, just bash and no perl/python/awk/etc. So in that sense maybe it offers something new to the discussion (or not). It calculates file size just once, but prints in various units (my preference). (The un-simplified version includes getopts that excludes "GB" if unwanted.)


printf -- ' %9s %9s %9s       %-30s\n' 'K'        'M'        'G'        'Path'
printf -- ' %9s %9s %9s       %-30s\n' '--------' '--------' '--------' '-----------'
du -sk "$@" | while read val; do
    file=$(echo "$val" | cut -f2-)
    size_k=$(echo "$val"  | cut -f1)
    printf ' %9s %9s %9s       %-30s\n' \
          ${size_k}  \
          $(( size_k / 1024 ))  \
          $(( size_k / 1024 / 1024 ))  \
  done | sort -n
share|improve this answer

du -s * | sort -nr | cut -f2 | xargs du -sh

share|improve this answer
That's not a great solution, because it traverses the file system twice. – Paul Gear Sep 30 '15 at 0:06

This is the alias I have in my .profile

alias du='sudo du -xh --max-depth=1 | sort -h'

sort -h is what really helps here to the question asked.

Another useful options are du -x to stay on the same filesystem; also sudo helps not to see errors if there are directories that aren't world-readable. Also, I always do du --max-depth=1, then drill down further etc..

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.