How do you sort du output by size?
If you have GNU coreutils (common in most Linux distributions), you can use
du -sh -- * | sort -h
The -h
option tells sort
that the input is the human-readable format (number with unit; 1024-based so that 1023 is considered less than 1K which happens to match what GNU du -h
does).
This feature was added to GNU Core Utilities 7.5 in Aug 2009.
Note:
If you are using an older version of Mac OSX, you need to install coreutils with
brew install coreutils
, then usegsort
as drop-in replacement ofsort
.Newer versions of macOS (verified on Mojave) support
sort -h
natively.
Try using the -k flag to count 1K blocks intead of using human-readable. Then, you have a common unit and can easily do a numeric sort.
du -ck | sort -n
You don't explictly require human units, but if you did, then there are a bunch of ways to do it. Many seem to use the 1K block technique above, and then make a second call to du.
https://serverfault.com/questions/62411/how-can-i-sort-du-h-output-by-size
If you want to see the KB units added, use:
du -k | sed -e 's_^\([0-9]*\)_\1 KB_' | sort -n
If you don't have a recent version of GNU coreutils, you can call du
without -h
to get sortable output, and produce human-friendly output with a little postprocessing. This has the advantage of working even if your version of du
doesn't have the -h
flag.
du -k | sort -n | awk '
function human(x) {
if (x<1000) {return x} else {x/=1024}
s="kMGTEPZY";
while (x>=1000 && length(s)>1)
{x/=1024; s=substr(s,2)}
return int(x+0.5) substr(s,1,1)
}
{gsub(/^[0-9]+/, human($1)); print}'
If you want SI suffixes (i.e. multiples of 1000 rather than 1024), change 1024 to 1000 in the while
loop body. (Note that that 1000 in the condition is intended, so that you get e.g. 1M
rather than 1000k
.)
If your du
has an option to display sizes in bytes (e.g. -b
or -B 1
— note that this may have the side effect of counting actual file sizes rather than disk usage), add a space to the beginning of s
(i.e. s=" kMGTEPYZ";
), or add if (x<1000) {return x} else {x/=1024}
at the beginning of the human
function.
Displaying a decimal digit for numbers in the range 1–10 is left as an exercise to the reader.