check disk space

Shell Script to Check Disk Space and Send Email Alerts

If your system has low disk space or runs out of disk space, then it will slow down your system. So it is advisable to monitor the amount of free disk space in your system from time to time. Linux allows you to write shell scripts to automate tons of tasks & processes. In this article, we will look at how to create shell script to check disk space and send email alerts in case you are running out of disk space.

Shell Script to Check Disk Space and Send Email Alerts

Here are the steps to check disk space and send email alerts to your system administrator.

1. Create Shell Script

Open terminal and create shell script file with the following command.

$ sudo vi /home/ubuntu/

2. Add Shell Script

Add the following shell script to this file. In the code below, replace THRESHOLD value with a threshold value of your choice, and with the email address of your system admin.


CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')


if [ "$CURRENT" -gt "$THRESHOLD" ] ; then

mail -s 'Disk Space Alert' << EOF

Your root partition remaining free space is critically low. Used: $CURRENT%



Save and exit the file. In the above code, we use df command to get free disk space information. Since the output of this command contains a lot of other information also, we use grep and awk commands to extract the exact disk usage number, and store it in CURRENT variable. We compare it with THRESHOLD number using an if condition, and send an email using mail command in case the CURRENT value is more than THRESHOLD.

3. Make Shell Script Executable

Run the following command to make your shell script executable.

$ sudo chmod +x /home/ubuntu/

4. Automate Script

Now we need to run this script hourly or at least daily. So we will create cronjob for it. Open crontab file with the following command.

$ crontab -e

Add the following line to run the above script daily.

@daily /home/ubuntu/

If you want to run the above script hourly, add the following line instead.

@hourly /home/ubuntu/

5. Test the Script

You can easily test this script by setting a lower disk space threshold, temporarily, such as 50% in the above script file. In this case, you will start receiving emails very soon.

That’s it. We have learnt how to easily measure the free disk space in our Linux system and send automated emails if your system is running out of disk space. It can be used in all Linux distributions since the commands in them are available in all Linux distributions. This script is very useful for nearly every system administrator.

Also read:

Shell Script to Send Email Alert When Memory Gets Low
Linux Split File into Multiple Files
How to Schedule Reboot in Linux
How to Redirect Nohup Output to File
How to Install Dependencies with APT

Leave a Reply

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