UNIX Get An Alert When Disk Is Full

I want to get an alert when my disk is full under UNIX and Mac OS X? How do I set a a specified threshold and run the script via cron?

The df command report file system disk space usage including the amount of disk space available on the file system containing each file name argument. Disk space is shown in 1K blocks by default, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.

Use df command and pass the -P option which make df output POSIX compliant (i.e. 512-byte blocks rather than the default. Note that this overrides the BLOCKSIZE specification from the environment).
# df -P /
# df -P /usr
Sample Outputs:

Filesystem    512-blocks     Used     Avail Capacity  Mounted on
/dev/aacd0s1e  162491344 21988048 127503992    15%    /usr

You can now simply grep /usr file system and print out used capacity:
# df -P /usr | grep /usr | awk '{ print $5}' | sed 's/%//g'
Or assign value to a variable:
# output=$(df -P /usr | grep /usr | awk '{ print $5}' | sed 's/%//g')
# echo $output

Under BASH or KornShell you can use arrays indexed by a numerical expression to make code small:
# output=($(df -P /))
# echo "${output[11]}"

A Sample Shell Script

# Tested Under FreeBSD and OS X
OUTPUT=($(LC_ALL=C df -P ${FS}))
CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
[ $CURRENT -gt $THRESHOLD ] && echo "$FS file system usage $CURRENT" | mail -s "$FS file system" you@example.com

You need to modify syntax, if you are using KSH or TCSH / CSH instead of BASH. Save this script and run as a cron job:
@daily /path/to/your.df.script.sh

GUI Notification

Display warning dialog using /usr/bin/zenity

# Tested Under FreeBSD and OS X
OUTPUT=($(LC_ALL=C df -P ${FS}))
CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
[ $CURRENT -gt $THRESHOLD ] && /usr/bin/zenity  --warning  --text="The disk $FS ($CURRENT% used) is almost full. Delete some files or add a new disk." --title="df Warning"
DF GUI Warning Notification

DF GUI Warning Notification

Finally update your cronjob as follows (you need to use DISPLAY variable to display output window):

36 19 * * *  DISPLAY=:0.0 /path/to/script.sh

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 28 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
28 comments… add one
  • Dariusz21p Aug 13, 2009 @ 10:18

    I think Linux desktop lack alarm in notification area about disks getting full just like you do in M$

    • manvstech Nov 29, 2011 @ 22:07

      Easy fix, change the last line of the script from:
      /usr/bin/zenity –warning –text=”The disk $FS ($CURRENT% used) is almost full. Delete some files or add a new disk.” –title=”df Warning”
      /usr/bin/zenity –notification –text=”The disk $FS ($CURRENT% used) is almost full. Delete some files or add a new disk.”

  • Shoaibi Aug 13, 2009 @ 10:35

    good one, though i use nagios, great tool.

    • 🐧 nixCraft Aug 13, 2009 @ 10:51

      nagios is good if you’ve large number of servers or workstation. For a single laptop, desktop or a small number of server script will save the time.


  • Vitor Castro Aug 13, 2009 @ 11:26

    Vivtek he’s right!

    Nagios only make sense when you have a LAN.

    That ‘s a good idea use that script but you can also send the output to the STDOUT with some blink text, for example.

    I did something like that a few years ago.

  • petrescs Aug 13, 2009 @ 13:35

    I think zenity (for Gnome) or kdialog (KDE) could be used to pop-up an info box, instead (or additionally) to sending an email.

  • 🐧 nixCraft Aug 13, 2009 @ 13:44


    The FAQ has been updated.

  • Jaysunn Aug 13, 2009 @ 15:54


    I was unaware of zenity. Very sweet.


  • Carmen Aug 13, 2009 @ 17:27

    I tried to use this script in my Ubuntu.
    # df -P /usr

    S.ficheros Bloques de 1024 Usado Libre Ocupado Montado en

    /dev/sda4 12399800 9341132 2433740 80% /

    # df -P /usr | grep /usr | awk ‘{ print $5}’ | sed ‘s/%//g’
    no answer…, and I was waiting for an output: 80
    What is wrong with that?

    Thanks in advance, I’m learning from you how to make scripts.

  • petrescs Aug 13, 2009 @ 17:53

    @Carmen: there’s no /usr in your case, just / (that is, root)
    Try df -P / | grep / | awk ‘{ print $5}’ | sed ‘s/%//g’ instead

  • Carmen Aug 13, 2009 @ 19:13

    Thank you!!
    That was very useful.

  • Jeroen Aug 14, 2009 @ 14:07

    Hello there,

    Now when I change this to the following ( note the space between the “M” and the “H” in “/media/FREECOM HDD”:

    # Tested Under FreeBSD and OS X
    # File systems ( partitions )
    FSfreecomhdd="/media/FREECOM HDD"
    # Everything above 85 will be given a yellow card for warning
    # Check given file systems for size
    for FS in $FSfreecomhdd ; do
    	echo $FS
    	OUTPUT=($(LC_ALL=C df -P $FS))
    	CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
    	[ $CURRENT -gt $THRESHOLD ] && /usr/bin/zenity  --warning  --text="The disk ${OUTPUT[12]} ($CURRENT% used) is almost full. Delete some files." --title="df Warning"

    it does not work anymore.

    Output will be :
    df: `/media/FREECOM’: No such file or directory
    df: no file systems processed
    ./check_partitions_sizes_simple.sh: line 15: [: -gt: unary operator expected
    df: `HDD’: No such file or directory
    df: no file systems processed
    ./check_partitions_sizes_simple.sh: line 15: [: -gt: unary operator expected

    With or without the escape character like FSfreecomhdd=”/media/FREECOM\ HDD” does not mather.

    The “df – P” will treat it as 2 strings which is obviously not my purpose.

    Is there another way to escape the space so that df will accept and treat this as 1 string in stead of 2 ??

    Thanks in advance,


    p.s. great website for shell programming and linux things !!!

  • 🐧 nixCraft Aug 14, 2009 @ 14:37

    This line should be corrected

    FSfreecomhdd="/media/FREECOM HDD"

    HDD is not a valid mount point, use full path like /usr /home /www

    FSfreecomhdd="/media/FREECOM / /home /www"
  • Jeroen Aug 14, 2009 @ 15:06

    Hai Vivek

    Thanks for the quick response on my email. Yes this mount point is automatically made for the usb drive under Fedora 8 at rebooting time. I suppose the usb drive is FAT32 formatted and given as label this name. I will find a way to give the usb drive a correct name without the spaces. Then it will be mounted correctly at startup. I suppose another way is to mount this usb drive manually under /media/freecomhdd for example.



  • petrescs Aug 14, 2009 @ 15:16

    @Jeroen: have not tried this, but it can help you https://help.ubuntu.com/community/RenameUSBDrive

  • Jeroen Aug 14, 2009 @ 15:50

    Hey petrescs,

    Yes this is great. I was banging my head on this because I relabelled an usb disk before. But then there was no data on it so I put a fat filesystem on it with the correct label. This usb drive has lots of data on it so I am a little bit more carefull with it. I did not know Gnome was able to do this but I am just checking this out with gParted and I it did the job oke.

    Thanks, great call,


  • Jeroen Aug 14, 2009 @ 17:49


    I used also the method of relabelling with mlabel and this works great and much faster then with gparted. I added an entry in the crontab but when this job runs it cannot display the warnings. I get this in the mail :
    (zenity:21175): Gtk-WARNING **: cannot open display:

    Is this something from Fedora 8 with crontab ??

    This was my entry :
    36 19 * * * /home/paula/shells/check_partitions_sizes.sh



  • petrescs Aug 14, 2009 @ 18:15

    @Jeroen: Try add “DISPLAY=:0.0” (those are zero, not lowercase O, and no quotes) as first line in your user crontab.

  • Bash Aug 14, 2009 @ 18:55

    You can get rid of sed like so:

    echo “${output[11]/\%/}”

  • 🐧 nixCraft Aug 14, 2009 @ 19:41


    You need to run it as follows /etc/profile.d/run.cron.gui.sh:

    # replace vivek with actual username, see xhost man page ;)
    /usr/bin/xhost +si:localuser:vivek

    Set permissions:

    chmod +x /etc/profile.d/run.cron.gui.sh

    Finally update your cronjob:

    36 19 * * *  DISPLAY=:0.0 /home/paula/shells/check_partitions_sizes.sh

    OR assuming that you are always on 0.0

    36 19 * * *  DISPLAY=:0.0 && /home/paula/shells/check_partitions_sizes.sh


  • Jeroen Aug 14, 2009 @ 20:35

    Woooohhhhh, I just wanted to react on this response : petrescs 08.14.09 at 6:15 pm
    That was a great one and did the trick….

    Vivek : I will try this tomorrow….its getting late here…

    Thanks every one,


  • Shantanu Oak Aug 15, 2009 @ 6:00

    How about twittering the alert?
    # Twit the disk status alert away
    [ $CURRENT -gt $THRESHOLD ] && curl –basic –user username:password –data status=”$FS file system usage $CURRENT” http://twitter.com/statuses/update.xml

  • Jeroen Aug 15, 2009 @ 22:20

    Hai Vivek and petrescs,

    The updating of the user cronjob worked fine so I changed my user crontab with the “DISPLAY=:0.0” . I am not sure what the extra value is of the /etc/profile.d/run.cron.gui.sh. With or without it my shell script starts and finishes and produces ( as far as I can see ) the same results.

    This “DISPLAY=:0.0” added to the cronjob solved for me other problems that output could not be showed on the display and gui applications could not be executed.

    Thank you @petrescs and @vivek for the answers to my questions. They were very helpful.


  • Mark Shields Nov 13, 2011 @ 17:37

    No need to use grep and awk together, at least not in the context you’re using it. And sed is a bit of an overkill; use tr instead.

    df -P /usr | awk '/\/usr/{ printf $5}' | tr -d "%"
  • Randall Oveson Mar 6, 2012 @ 21:07

    Thanks for the script, works great!

  • aj Nov 19, 2012 @ 15:16

    I using this scripts and i am getting this following error

    # vi full.ksh
    “full.ksh” 7 lines, 377 characters
    set -x
    # Tested Under FreeBSD and OS X
    OUTPUT=($(LC_ALL=C df -P ${FS}))
    CURRENT=$(echo ${OUTPUT[11]} | sed ‘s/%//’)
    [ $CURRENT -gt $THRESHOLD ] && /usr/bin/zenity –warning –text=”The disk $FS ($CURRENT% used) is almost full. Delete some files or add a new disk.” — ti
    tle=”df Warning” | mail -s “$FS file system” ak@mountsaintvincent.edu

    # sh full.ksh
    full.ksh: syntax error at line 6: `OUTPUT=’ unexpected

  • Joe Bazin Nov 23, 2013 @ 21:01

    I am looking for shell script which can be scheduled as cronjob under root to do the following on server named abcz on AIX platform.

    The script should capture the unsuccessful login attempts for user ID joebazin & oracle with the details -ip address, attempt time and number of attempts.

  • foobar Oct 28, 2014 @ 22:57

    Use -P for POSIX standard:

    $ df -P | awk ‘/\/home$/{ gsub(/%/,””); print $5 }’

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum