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?
-h
option? It's the same as du's -h – glenn jackman Apr 21 '15 at 01:02du -sh * | sort -gh
? – nonopolarity Apr 21 '15 at 01:07du -sk * | sort -n
which lists the sizes in kilobytes. I find that easier to read than raw bytes. – Greg Hewgill Apr 21 '15 at 01:14