1

On a Mac, using Bash, I tend to do

du -sh *

to tell the sizes of files and folders in the current directory, in MB and GB. But if I want to sort them by the sizes, I might do

du -sh * | sort -g

and it won't work well, because the M and G is not taken into account (so 44MB might be thought to be larger than 20GB, because 44 is greater than 20).

Is there a way not to write a program and just use UNIX commands and/or a Bash function to do that?

If I care about the files and folders that are in the GB range, I can do:

du -sh * | egrep "^\s*\d+(.\d+)?G\s" | sort -g

and it will show

2.6G    ski video clips
7.6G    trip 2012.mp4
 12G    trip photos  

but if TB or MB is to be considered, then the above will not work. Is there a way to do this more generally using UNIX / Mac OS X commands?

nonopolarity
  • 3,069

1 Answers1

1

http://www.commandlinefu.com/commands/view/12927/list-the-size-in-human-readable-form-of-all-sub-folders-from-the-current-location says:

du -ks $(ls -d */) | sort -nr | cut -f2 | xargs -d '\n' du -sh 2> /dev/null

This is tested on a Mac and only runs du once. It has the minor error that 1K=1000 and not 1024:

du -sk * | sort -n | perl -pe '@SI=qw(K M G T P); s:^(\d+?)((\d\d\d)*)\s:$1.$SI[((length $2)/3)]."\t":e'
Ole Tange
  • 35,514