Home > Bash, linux, Programming > Display disk usage in colors

Display disk usage in colors

#!/bin/bash
LIMIT=85
usage() {
  echo "usage:"
  echo -e "\t$0 threshold [ other df options ]"
  echo -e "\t$0 -H : for help with usage of this script"	
}
if [ $# -lt 1 ]; then
  echo "**displaying disk usage with default threshold=$LIMIT, \
see mydf -H for usage**"
else
  if [[ $1 =~ ^[0-9]{1,3}$ ]]; then  
    LIMIT=$1
  else 
    usage
    exit 0
  fi
fi
shift
df -P $* | awk "BEGIN{FS=\"[ %]+\"} {
  if (NR==1) print \$0; 
  else if (\$5 > $LIMIT) print \"33[31m\"\$0\"33[0m\"; 
  else print \"33[32m\"\$0\"33[0m\"}"

 

Note: this script has been tested under ubuntu, opensuse, gentoo and freebsd. Example usages:
mydf
  displays df in colors with default threshold of 85%
mydf 75 -h
  displays df in colors with threshold of 75% and capacity units in human-readable format.

[ direct link: http://rc3.fileave.com/mydf.sh ]

Screenshot from my own desktop:

mydf.sh example output screenshot

mydf.sh example output screenshot

Advertisements
Categories: Bash, linux, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: