Disk usage from du/df with colored graph output

Here’s a nifty little command that I picked up off my commandlinefu.com RSS feed a couple weeks back for colored graph of du

t=$(df|awk ‘NR!=1{sum+=$2}END{print sum}’);du / –exclude /proc –exclude /sys –max-depth=1|sed ‘$d’|sort -rn -k1 | awk -v t=$t ‘OFMT=”%d” {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x<b;x++) {printf “\033[1;31m” “|” “\033[0m”}print ” “$2″ “(a/t*100)”% total”}’

Run As Root! This is meant to be used as a one-liner, it’s broken up here to make it readable on the page

Yea it’s a long one. Basically it takes the output from df and du / ( I’ve added some exclusions for /proc and /sys to quiet down the errors ) sorts it, and outputs a rudimentary ASCII graph showing the percentage of disk usage for / level directories ( home, etc, opt, tmp ).

You can change the directories it scans by tweaking the du c0mmand at the start, and you can also tweak the colors the graph outputs by tinkering with these:  printf “\033[1;31m” “|” “\033[0m”}.

Here’s a quick list of ANSI/bash color codes to use:

Black 0;30

Dark Gray 1;30

Blue 0;34

Light Blue 1;34

Green 0;32

Light Green 1;

32 Cyan 0;36

Light Cyan 1;

36 Red 0;31

Light Red 1;31

Purple 0;35

Light Purple 1;35

Brown 0;33

Yellow 1;33

Light Gray 0;37

White 1;37

Test it out in a terminal, if you don’t find it useful I guarantee you’ll find it at least mildly entertaining.