Check remaining disk space

The following commands will check to see if you are running out of disk space and warn you when any partition has 20% or less free space.

_space=$(df -T|grep -v "tmpfs"|grep -v "iso"|cut -d "%" -f1|rev|cut -d " " -f1|rev|sort -n|tail -1)
case "$_space" in 100) echo 'Alert: Disk full!!';;[89][0-9]) echo "Warning: Disk space low... ($_space%)";;esac

I find it useful to include these commands in my .bashrc file so they run automatically. If you want a more granular warning messages you can add additional tests into the case statement. In the example below I’ve added an additional test to check to see if any partition has less then 5% space remaining, and included a couple of escape characters to provide an audiable warning (which usually only works on the console).

case "$_space" in 100) echo 'Alert: Disk full!!';;[9][5-9]) echo $'\bCaution: Less than 5% disk space remaining\b';;[89][0-9]) echo "Warning: Disk space low... ($_space%)";;esac
Advertisements
This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

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