Bash Script: Monitoring Disk Usage

This post teaches beginners how to write a Bash script that monitors disk usage across all mounted filesystems. It covers the df command, parsing its output with awk and sed, setting alert thresholds, and scheduling the script with cron. A bonus section shows how to send email alerts when disk usag

Bash Script: Monitoring Disk Usage

Disk space has a habit of disappearing quietly. A log directory fills up, a database grows unchecked, or a developer drops a massive archive somewhere and forgets about it. Before you know it, a critical service crashes because the filesystem is full. A simple Bash script that monitors disk usage can catch these problems early and keep you ahead of the curve.

Understanding the df Command

The foundation of any disk monitoring script is the df command (disk free). Run it on its own and you get a snapshot of every mounted filesystem:

df -h

The -h flag makes the output human-readable, converting bytes into KB, MB, and GB. A typical output looks like this:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   34G   14G  71% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
/dev/sdb1       200G  180G   20G  90% /data

The column you care most about is Use%. That percentage is what your script will check against a threshold you define.

Building the Monitoring Script

Here is a practical script that checks disk usage on all mounted filesystems and alerts you when any of them exceed a threshold:

#!/bin/bash

# Disk Usage Monitor
# Alerts when any filesystem exceeds the defined threshold

THRESHOLD=80
DATE=$(date '+%Y-%m-%d %H:%M:%S')

echo "Disk Usage Report - $DATE"
echo "================================"

df -h | grep -vE '^Filesystem|tmpfs|udev' | while read -r line; do
    usage=$(echo "$line" | awk '{print $5}' | sed 's/%//')
    filesystem=$(echo "$line" | awk '{print $1}')
    mount=$(echo "$line" | awk '{print $6}')

    if [ "$usage" -gt "$THRESHOLD" ]; then
        echo "WARNING: $filesystem mounted at $mount is at ${usage}% usage!"
    else
        echo "OK: $filesystem mounted at $mount is at ${usage}% usage."
    fi
done

Let's walk through what each part does:

  • THRESHOLD=80: Sets the alert level. Anything above 80% triggers a warning. You can change this to suit your environment.
  • df -h | grep -vE '^Filesystem|tmpfs|udev': Runs df and filters out the header line plus temporary filesystems that don't need monitoring.
  • awk '{print $5}': Extracts the fifth column, which is the usage percentage.
  • sed 's/%//': Strips the percent sign so Bash can compare it as a number.
  • if [ "$usage" -gt "$THRESHOLD" ]: Compares the usage integer against your threshold and prints either a warning or an OK message.

Making the Script Executable and Scheduling It

Save the script as disk_monitor.sh, then make it executable:

chmod +x disk_monitor.sh

Run it manually to confirm it works:

./disk_monitor.sh

To make it useful, schedule it to run automatically using cron. Open your crontab:

crontab -e

Add a line to run the script every hour and append the output to a log file:

0 * * * * /home/youruser/disk_monitor.sh >> /var/log/disk_usage.log 2>&1

Taking It Further: Email Alerts

Logging is useful, but alerts sent directly to your inbox are even better. If your system has mail or sendmail configured, you can pipe warnings straight to an email address by replacing the echo warning line with:

echo "WARNING: $filesystem at $mount is ${usage}% full" | mail -s "Disk Alert on $(hostname)" [email protected]

This small addition turns a passive script into an active notification system.

What's Next

Now that you can monitor disk usage, the natural next step is expanding your scripting toolkit to monitor other system resources. In the next post, we'll look at how to monitor CPU and memory usage with Bash, giving you a more complete picture of your system's health. Together, these scripts form the foundation of a lightweight, homegrown monitoring solution you can run on any Linux server.

🔧
If you want to take disk monitoring beyond cron jobs and log files, PRTG Network Monitor gives you a GUI-based dashboard with built-in disk usage sensors, threshold alerts, and email notifications out of the box — no scripting required. PRTG Network Monitor, Zabbix and Nagios.