I'm trying to use du to sort the output by filesize, but it doesn't looks like there's an option for that.

Can I pipe it into another command to handle the sorting?


Shorter, and more succinct (and no Perl ;) ):

du -sm | sort -nr
  • 2
    Gnu sort has an option -h to sort human-readable SI numbers, with 4K being smaller than 1M. It's an option when -m/-g/-k don't work well. – Daniel Beck Nov 28 '12 at 19:57
  • Nice, +1, you don't need the * though, du will run on the current directory by default. – terdon Nov 29 '12 at 13:26

Try 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}'\'

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.