Advertisement

The Script

Paste this into a file called diskcheck.sh and you're set. Change THRESHOLD to whatever percentage you want to be warned at.

diskcheck.sh
#!/bin/bash
# Disk Space Warning
# Checks disk usage and warns if over 80%.
# Great for daily cron on a server or VPS.
#
# USAGE: ./diskcheck.sh
# REQUIRES: bash, df, awk, tr (all pre-installed on Linux/macOS)

THRESHOLD=80   # ← warn when disk usage hits this %
PARTITION="/"  # ← which partition to check (/ = root)

USAGE=$(df "$PARTITION" | awk 'NR==2{print $5}' | tr -d '%')

if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "⚠ WARNING: Disk at ${USAGE}% — time to clean up"
else
  echo "✓ Disk OK: ${USAGE}% used (threshold: ${THRESHOLD}%)"
fi
✓ What this does, line by line

df "$PARTITION" shows disk stats for your chosen partition. awk 'NR==2{print $5}' grabs the 5th column (usage %) from the second row. tr -d '%' strips the percent sign so the number can be compared mathematically. The if block then prints a warning or an all-clear.

Step-by-Step Setup

Step 1 — Create the file

terminal
nano diskcheck.sh

Paste the script, then press Ctrl+X → Y → Enter to save.

Step 2 — Set your threshold

Change THRESHOLD=80 to match your needs:

ThresholdWhen to use it
THRESHOLD=70Servers with fast-growing logs or databases — catch it early
THRESHOLD=80General purpose — the sensible default for most setups
THRESHOLD=90Storage servers where 90% full is normal and expected

Step 3 — Make it executable

terminal
chmod +x diskcheck.sh

Step 4 — Run it manually first

terminal
./diskcheck.sh

You'll see either: ✓ Disk OK: 43% used (threshold: 80%) or a warning if you're already over.

Advertisement

Schedule It with Cron

This script is useless if you only run it manually. The whole point is to let it run in the background and catch problems before they happen.

Open your crontab

terminal
crontab -e

Pick your schedule

crontab
# Check every hour
0 * * * * /home/user/diskcheck.sh

# Check every day at 8am
0 8 * * * /home/user/diskcheck.sh

# Check every 5 minutes (high-traffic servers)
*/5 * * * * /home/user/diskcheck.sh
💡 Use the full path in cron

Cron doesn't know where your script lives unless you tell it. Always use the full path like /home/user/diskcheck.sh, not just ./diskcheck.sh.

Variations

Check multiple partitions at once

Servers often have separate partitions for /var, /home, or /data. Check them all:

diskcheck-multi.sh
#!/bin/bash
THRESHOLD=80
PARTITIONS=("/" "/var" "/home")

for p in "${PARTITIONS[@]}"; do
  # Skip if partition doesn't exist
  mountpoint -q "$p" || continue

  USAGE=$(df "$p" | awk 'NR==2{print $5}' | tr -d '%')
  if [ "$USAGE" -gt "$THRESHOLD" ]; then
    echo "⚠ $p is at ${USAGE}% — above ${THRESHOLD}% threshold"
  else
    echo "✓ $p is OK: ${USAGE}%"
  fi
done

Send an email alert instead of just printing

More useful than a print statement — sends you an actual email when the disk is almost full:

diskcheck-email.sh
#!/bin/bash
THRESHOLD=80
EMAIL="you@example.com"  # ← your email address
USAGE=$(df / | awk 'NR==2{print $5}' | tr -d '%')

if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "Disk is at ${USAGE}% on $(hostname). Clean it up." \
    | mail -s "⚠ Disk Warning: ${USAGE}% on $(hostname)" "$EMAIL"
fi
💡 Install mail if it's missing

On Ubuntu/Debian: sudo apt install mailutils. On CentOS/RHEL: sudo yum install mailx. Then test it by running: echo "test" | mail -s "test" you@example.com

Show a full disk report

A more detailed version that shows all mounted drives at once:

diskcheck-report.sh
#!/bin/bash
# Print a clean disk usage report for all real partitions
echo "=== Disk Space Report: $(date) ==="
df -h --output=target,used,avail,pcent \
  | grep -v tmpfs \
  | grep -v udev
echo "=================================="
Monitor your server — not just your disk DigitalOcean includes built-in CPU, RAM, and disk alerts. Deploy a droplet from $4/month and get $200 free credit for new accounts.
Get $200 Free →
DigitalOcean Referral Badge

Common Mistakes

⚠ Checking the wrong partition

On servers with separate mount points, df / only tells you about the root partition. If your logs live on /var, you need df /var. Run df -h with no arguments to see all partitions and their usage at once.

⚠ The script warns but nobody sees it

If you're running this via cron with no email configured, the output goes nowhere. Either set up email alerts (see variation above) or redirect output to a log file: add >> /var/log/diskcheck.log 2>&1 to your cron line.

⚠ df output differs by OS

This script targets Linux. On macOS, the df output format is slightly different. Replace the awk command with: df -P / | awk 'NR==2{print $5}' | tr -d '%' for cross-platform compatibility.

Advertisement

Understanding the Commands

Here's a breakdown of each command used in this script so you can adapt it confidently:

CommandWhat it does
df /Shows disk space stats for the root partition in 1K blocks
df -h /Same as above but human-readable (shows GB/MB instead of blocks)
awk 'NR==2{print $5}'Grabs the 5th column from line 2 — which is the "Use%" column
tr -d '%'Strips the percent sign so the number can be used in math comparisons
[ "$USAGE" -gt "$THRESHOLD" ]Integer comparison: is usage greater than threshold?

Frequently Asked Questions

How do I check disk space in Linux with bash?

Use the df command. Running df -h / shows human-readable disk usage for your root partition. To extract just the percentage as a number you can use in a script: df / | awk 'NR==2{print $5}' | tr -d '%'

How do I get an alert when disk space is low?

Write a bash script that reads the df output, compares it to a threshold using an if statement, and sends an email with mail when it trips. Schedule it with cron to run automatically on a repeating interval.

How do I send an email alert from a bash script?

Use the mail command: echo "Disk almost full!" | mail -s "Disk Warning" you@example.com. You may need to install it first: sudo apt install mailutils on Ubuntu/Debian.

What does the df / command do in Linux?

df stands for "disk free." Running df / shows the disk space statistics for your root filesystem — total size, used space, available space, and the usage percentage. Add the -h flag for human-readable output in GB and MB.

Related Snippets