Monitor UNIX / Linux Server Disk Space with Shell Script

Shell script to monitor or watch the disk space and send an email alert if the (free avilable) percentage of space is >= 90%

# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage 
# of space is >= 90% 
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# ----------------------------------------------------------------------
# Linux shell script to watch disk space (should work on other UNIX oses )
# set admin email so that you can get email
# set alert level 90% is default
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | 
     mail -s "Alert: Almost out of disk space $usep" $ADMIN
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
44 comments… add one
  • Gouthami Nov 4, 2020 @ 10:37

    I am looking for a script should be finding the quota soft and hard limits and also send out an email

  • sahana Feb 10, 2017 @ 9:36

    Hi All ,
    Good script , I also have a requirement to clear old logs created before one month from say today to clear space if the alert is reached . Please help me ..

  • Learning DBA Oct 3, 2016 @ 12:01

    I am getting an error

    Syntax error: “fi” unexpected

    Please Help!!!

  • Rocky Islam Jul 2, 2015 @ 10:09

    i’m new in scripting … using this command i didn’t get any mail ….. help me

  • omprakash Dec 6, 2013 @ 7:44

    i want 5 mail alert instead of many alert when disk space reached max limit .

  • Leszek Nov 11, 2013 @ 15:50


    Thanks for the script. It’s doing it’s job nicely but I’m a little surprised nobody complained about an error it returns if You don’t cut off the first line of df output:

    ./ 22: [: Illegal number: dost.

    I had to change line 19 to:

    df -H | grep -vE ‘^Filesystem|tmpfs|cdrom’ | tail -n+2 | awk ‘{ print $5 ” ” $1 }’ | while read output;

  • Scriptiee Jul 11, 2013 @ 7:42

    Brilliant little script, will put it to good use! Thanks guys

  • Yamaranews Mar 30, 2013 @ 8:13

    Its very helful
    Thank you

  • KC Apr 27, 2012 @ 13:04

    Thanks, VIVEK and BEN. Very nice script and the quick correction, thanks a lot.

  • Andre Oct 19, 2011 @ 8:24

    Just answered my own question after a few days of figuring out bash regex. Are there any other more elegant ways?

    # set alert level 95% is default
    # The current variable takes from a file which gives the prior saved value of usage
    current=`cat /tmp/currentUsage`

    df -PH | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $1 }’ | while read output;
    usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1)
    partition=$(echo $output | awk ‘{ print $2 }’ )

    # If current usage is greater than ALERT and is greater the prior value then send a mail.

    if [ $usep -ge $ALERT ] && [ $usep -gt $current ]; then
    echo “Running out of space \”$partition ($usep%)\” on $(hostname) as on $(date)” |
    mail -s “Alert: Almost out of disk space $partition $usep%” $ADMIN

    # establish the new current value
    echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 > /tmp/currentUsage

  • Andre Oct 18, 2011 @ 1:48


    Right now, I have setup a cronjob to run this script hourly.
    Does anyone know how to stop the email to be sent out if the previous quota level is the same as the current one?

    i.e.: An email is sent once it hits the 95% threshold; however at the next hour if the level is still at 95%, no email is sent till it hits 96% or more.

  • Dinesh Jul 6, 2011 @ 7:25


    I am new to scripting.
    I have run this and working perfectly.
    Just want 2 modifications.

    If i want to check for more than one server using same script.
    And i want single mail instead of many.

    Please Help. :)

  • Anon Apr 20, 2011 @ 16:18

    I have tried the script and it does not appear to work.

    I set the limit to “35”, as I wanted to test it out and the highest percentage usage was 38%. I changed the E-Mail address to what I want to use and then ran the script. The script has not sent me an E-Mail after 10 minutes of running it.

    I tried the “-P” that was mentioned in several comments without luck.

    The server that this script is currently running on is a basic Ubuntu 9.04 server and has only had SSH installed.

  • Anonymous Mar 24, 2011 @ 14:59

    Thanks UWAYO.
    -P option worked with the script. Nice and simple script..

  • Ben Oct 19, 2010 @ 14:11

    you should also use the -P option of df “df -P -H”
    (-P for POSIX compatibility so you dont get into trouble when your diskname ist to long like /dev/mapper/VolGroup00-LogVol00)
    also df -i (for inodes) is highly recommended to use, because you easily forget to check for inodes if you have free space but cant create a file =)

    • nicole Jan 17, 2011 @ 4:53

      Hi, i’m a newbie, and i’ve been asked to create a script that will monitor different directories in different systems e.g.:

      /home of system1 – 60%
      /models of system2 – 70%
      /bikes of system3 – 80%

      and any of the above meet their respective threshold, will send an email to me
      informing that that particular directory is on limit.

      fyi, i have a server than can ssh to all of this directories/systems.

      hoping for your quick response.

  • UWAYO Jacques Sep 8, 2010 @ 18:32

    i get the below error what could be the problem

    but i dont receive anything
    # script to send simple email
    # email subject
    # Email To ?
    # Email text/message
    echo “This is an email message test”> $EMAILMESSAGE
    echo “This is email text” >>$EMAILMESSAGE
    # send an email using /bin/mail
    /bin/mail -s “$SUBJECT” “$EMAIL” < $EMAILMESSAGE

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.