Linux script to check if your hard disk is nearly full

Here is a simple script that will check if your specified partition is almost full and will notify you if it is. In order to have an email send, you must have mailx installed, for installation check my post here.

  • change user@mail.com with your email
  • change /dev/sda1 with the partition you want to monitor
  • change threshold value with the desired one.
    eg. If you want to get notifications if your specified partition is more than 90% full, enter 90
#!/bin/bash

#start editing
mail="user@mail.com"
partition="/dev/sda1"
threshold="90"
#stop editing

percent=$(df -h | grep "$partition" | awk '{ print $5 }' | sed 's/%//g')
if ((percent > threshold))
then
echo "$partition at $(hostname -f) is nearly full" | mail -s "Your disk is nearly full!!!" "$mail"
fi

You can place it in crontab and have it executed automatically
eg. to check once every day, insert into cron

23 6 * * * /path/to/script
  1. it works. thanks.

  2. Thanks for your script 🙂 it work’s on Fedora.

  3. Awesome, works in ubuntu 14.04

  4. df -h / will only give you the disk usage results for the root partition, perhaps use df -h

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.