Human readable disk usage

Finding out what directories and files are using up the disk space on your server is fairly easy with du, but the output is not always easy to read.

However, it’s not too hard to pretty up the output with some perl and the module Number::Bytes::Human (available from CPAN). To convert normal du output into a more human readable form, which shows file size in the correct units (K, M, or G) and also includes the percentage of the total space each entry is using, use the follwing steps.

Finding out what directories and files are using up the disk space on your server is fairly easy with du, but the output is not always easy to read.

However, it’s not too hard to pretty up the output with some perl and the module Number::Bytes::Human (available from CPAN). To convert normal du output into a more human readable form, which shows file size in the correct units (K, M, or G) and also includes the percentage of the total space each entry is using, use the follwing steps.

First, you’ll want to generate a file with sorted du output of the partition you’d like to examine with ‘du -x $PARTITION | sort -n > /tmp/$PARTITION.du.

Second, you’ll need to find the total used space with ‘tail -n1 /tmp/$PARTITION.du’, and substitute it for $TOTAL_SPACE in the next step.

Finally, you’ll want to run your du output through a (somewhat ugly) perl one-liner to convert it to something more readable.

cat /tmp/$PARTITION.du | perl -mNumber::Bytes::Human=format_bytes -nle ‘if(/(d+)s+(.+)$/) { printf(“%4s (%*.2f%%) %s %sn”, format_bytes($1*1024), 6, ($1 / $TOTAL_SPACE * 100), $2); }’

Leave a Reply

Your email address will not be published. Required fields are marked *