du with colored bar graph

du -x --max-depth=1|sort -rn|awk -F / -v c=$COLUMNS 'NR==1{t=$1} NR>1{r=int($1/t*c+.5); b="\033[1;31m"; for (i=0; i<r; i++) b=b"#"; printf " %5.2f%% %s\033[0m %s\n", $1/t*100, b, $2}'|tac
A more efficient way, with reversed order to put the focus in the big ones.
Sample Output
 41.37% ############################################################## Others
 17.96% ########################### Woody Allen
 13.19% #################### Argentina
 10.86% ################ Ingmar Bergman
  6.55% ########## Serbia
  3.92% ###### NI
  2.24% ### Luis Bu?uel
  0.95% # Jean Renoir
  0.66% # M?xico
  0.64% # Colombia
  0.45% # Charlie Chaplin
  0.45% # Leni Riefenstahl
  0.42% # Cuba
  0.36% # Chile

11
2015-09-12 10:36:49

1 Alternatives + Submit Alt

  • i'm using gawk, you may get varying mileage with other varieties. You might want to change the / after du to say, /home/ or /var or something, otherwise this command might take quite some time to complete. Sorry it's so obsfucated, I had to turn a script into a one-liner under 255 characters for commandlinefu. Note: the bar ratio is relative, so the highest ratio of the total disk, "anchors" the rest of the graph. EDIT: the math was slightly wrong, fixed it. Also, made it compliant with older versions of df. Show Sample Output


    13
    t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');sudo du / --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"}'
    kevinquinnyo · 2011-12-01 01:21:11 9

What Others Think

Pretty, but you can shrink the awk further by changing the bar character. At the moment, you loop to create enough red #s. The alternative is to let printf generate a wide-enough string using the %*s format. Then use the escape sequence Reverse (7) to make it visible. . Here's the awk (saving 37 characters :-) ... | awk -F / -v c=$COLUMNS 'NR==1{t=$1}NR>1{r=c*$1/t+0.5;printf " %5.2f%% \033[1;7;31m%*s\033[0m %s\n",100*$1/t,r,"",$2}' | ... . I've also dropped the int() which wasn't needed, at least for Gnu awk. . du -x --max-depth=1|sort -rn|awk -F / -v c=$COLUMNS 'NR==1{t=$1}NR>1{r=c*$1/t+0.5;printf " %5.2f%% \033[1;7;31m%*s\033[0m %s\n",100*$1/t,r,"",$2}'|tac
flatcap · 144 weeks and 5 days ago
Any ideas why it works on commandline but not when put into a script (and executed)?
wejn · 144 weeks and 5 days ago
Oh, nevermind. All you gotta do is to prepend: COLUMNS=${COLUMNS-$(tput cols)}
wejn · 144 weeks and 5 days ago
Nice. The second one doesn't work for me, just seems to hang. The first works, though. Wouldn't mind something similar for df :-)
sophiejane · 144 weeks and 4 days ago
Nice. The second one doesn't work for me, just seems to hang. The first works, though. Wouldn't mind something similar for df :-)
sophiejane · 144 weeks and 4 days ago
Nice. The second one doesn't work for me, just seems to hang. The first works, though. Wouldn't mind something similar for df :-)
sophiejane · 144 weeks and 4 days ago
ncdu is best, if you can have ncurses. kdirstat on x11.
oernii2 · 143 weeks and 6 days ago
On a mac update coreutils to get the max-depth parameter: ➜ ~ brew install coreutils ➜ ~ t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');sudo du / --max-depth=1|sed '$d'|sort -rn -k1 | awk -v t=$t 'OFMT="%d" {M=64; for (a=0;ac){c=a}}br=a/c;b=M*br;for(x=0;x /usr/local/opt/coreutils/libexec/gnubin/du: WARNING: Circular directory structure. This almost certainly means that you have a corrupted file system. NOTIFY YOUR SYSTEM MANAGER. The following directory is part of the cycle: ?/dev/fd/3? /usr/local/opt/coreutils/libexec/gnubin/du: WARNING: Circular directory structure. This almost certainly means that you have a corrupted file system. NOTIFY YOUR SYSTEM MANAGER. The following directory is part of the cycle: ?/dev/fd/4? /usr/local/opt/coreutils/libexec/gnubin/du: cannot read directory ?/dev/fd/5?: Not a directory /usr/local/opt/coreutils/libexec/gnubin/du: cannot read directory ?/dev/fd/6?: Not a directory ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| /Users 34.6041% total |||||||||||| /private 6.16793% total |||||| /opt 3.06745% total ||||| /Applications 2.69127% total ||| /Library 1.52773% total ||| /.Spotlight-V100 1.51995% total ||| /System 1.42486% total ||| /Developer 1.36405% total || /usr 0.548598% total | /.DocumentRevisions-V100 0.00531854% total | /.fseventsd 0.00265599% total | /bin 0.00156543% total | /sbin 0.000959563% total | /dev 2.04685e-06% total | /Volumes 1.63748e-06% total | /net 4.0937e-07% total | /home 4.0937e-07% total /cores 0% total /Network 0% total /.vol 0% total /.Trashes 0% total
AlainG80 · 112 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: