Skip to content

Instantly share code, notes, and snippets.

@jantoniomartin
Created December 16, 2011 13:11
Show Gist options
  • Save jantoniomartin/1485996 to your computer and use it in GitHub Desktop.
Save jantoniomartin/1485996 to your computer and use it in GitHub Desktop.
Just for fun, a little script to show a colored output of df
#! /bin/bash
## Just for fun, a little script to show a colored output of df
## to get a prettier view of disk usage
HIGH=80
MEDIUM=50
FIRST=0
DEFAULT_OPTS="-h -x tmpfs -x rootfs -x devtmpfs"
if [ $# -eq 0 ]
then
OPTIONS=$DEFAULT_OPTS
else
OPTIONS=$*
fi
## colors
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtred=$(tput setaf 1) # Red
txtblk=$(tput setaf 7) # White
while read line
do
read -r system size used avail use mounted <<<"$line"
if [ $FIRST -ne 0 ]
then
percent=`echo $use | sed 's/^\([0-9]*\)%$/\1/'`
color=$txtgrn
if [ $percent -ge $MEDIUM ]
then
color=$txtylw
fi
if [ $percent -ge $HIGH ]
then
color=$txtred
fi
else
system="Filesystem"
size="Size"
used="Used"
avail="Avail"
percent="Use"
mounted="Mounted"
FIRST=1
color=$txtblk
fi
echo "$system $mounted $size $used $avail ${color}$percent%${txtblk}"
done <<< "`df $OPTIONS`" | column -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment