Bash Script to Monitor Disk Usage

This script will check disk usage on / partition and email you if disk usage is above 80%

We can use df / command to find current disk usage

As you can see, the result have 2 lines. We don’t need first line. To ignore first line, we can use

From the result, we only need the Use% part. In this case 66%, to find this, you can use awk command, that split the line, then prient specified part. In our case, we need 5th part.

To use disk usage % in our script for calculation, we need it converted to number. That is remove %. This can be done with sed command

Now we have disk usage as a number. We can use it in our script.

Add following content to it

In above script, replace YOUR_EMAIL_HERE with your email address.

disk usage bash script

You can run the script daily using cronjob. If disk usage ever go above 80%, you will get email alert.

See bash

Leave a Reply

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