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.
What this does, line by line
THRESHOLD=80— Alert when usage goes above 80%. Change this to match your policy.PARTITION="/"— Which mount point to check. Use/varfor a web server with large logs, or/homefor user data.df "$PARTITION"— Reports disk space for that partition.awk 'NR==2 {print $5}'— Grabs the Use% column from the second line ofdfoutput.tr -d '%'— Strips the percent sign so you can compare numbers in theiftest.[ "$USAGE" -gt "$THRESHOLD" ]— Bash integer comparison. Exits with code 1 on warning (useful for cron alerting).
Step-by-Step Setup
Step 1: Create the script file
Paste the script above. Press Ctrl+X, then Y, then Enter to save.
Step 2: Set your threshold
| Threshold | Best for |
|---|---|
| 70% | Early warning on busy servers — gives you time to clean up before it's critical |
| 80% | General-purpose default — good balance for most Linux VPS and home servers |
| 90% | Last-chance alert — only when you want minimal noise and will act immediately |
Edit the THRESHOLD= line at the top of the script to match your choice.
Step 3: Make it executable
Step 4: Run it manually
When usage is below the threshold:
When usage is above the threshold:
Schedule It with Cron
Running the script by hand only helps when you remember. For a server, put it on a schedule with cron so you catch problems early.
Open your crontab:
Add one of these lines (adjust the path to where you saved diskcheck.sh):
Use the full path in cron
Cron runs with a minimal environment. Always use the full path to your script (e.g. /home/you/scripts/diskcheck.sh), not ./diskcheck.sh. If the script calls other commands, set PATH at the top of the script or use full paths for those too.
Variations
Check multiple partitions
Use this version when you need to monitor /, /var, and /home in one run:
Save as diskcheck-multi.sh, then chmod +x diskcheck-multi.sh.
Send email alert instead of echo
Requires mailutils (Debian/Ubuntu: sudo apt install mailutils). Save as diskcheck-email.sh.
Common Mistakes
Checking the wrong partition
On many servers, /var or /home fills up before /. If you only run df /, you can miss a full log or upload directory. Match PARTITION to where data actually grows — e.g. PARTITION="/var" for Apache/nginx logs.
Script warns but nobody sees it
Cron runs in the background. If you only echo to stdout, output may go to email (if MAILTO is set) or nowhere. Redirect output to a log you check:
Or use the email variation above.
df output differs by OS
On macOS, df can wrap columns differently. Use POSIX mode for consistent parsing:
The -P flag forces POSIX output (one line per filesystem).
Understanding the Commands
| Command | What it does |
|---|---|
df / | Disk free — shows used/available space for the root filesystem |
df -h / | Same as df / but human-readable sizes (GB, MB) |
awk 'NR==2{print $5}' | Prints field 5 from line 2 — the Use% column |
tr -d '%' | Deletes % characters so 85% becomes 85 for numeric compare |
[ "$USAGE" -gt "$THRESHOLD" ] | True when usage is greater than your threshold |
Frequently Asked Questions
How do I check disk space in Linux with bash?
Use df (disk free). For a quick human-readable check:
To extract just the usage percentage for scripting:
Combine with THRESHOLD and an if statement — that's what diskcheck.sh does.
How do I get an email when my disk is full?
- Install mail tools:
sudo apt install mailutils - Use the email variation script with your address in
EMAIL= - Schedule with cron so it runs hourly or daily
The mail command sends the script output as the message body.
How do I get an alert when disk space is low?
Bash + threshold + cron is the standard pattern: this script compares usage to THRESHOLD, prints or mails on breach, and cron runs it on a schedule. For SMS or Slack, pipe the warning into curl to a webhook instead of echo.
How do I send an email alert from a bash script?
Use a meaningful subject (Disk alert: / at 87%) so you can filter inbox rules.
What does the df / command do in Linux?
df means disk free. It reports filesystem size, used space, available space, and mount point. The / argument limits output to the root filesystem. Columns are typically: Filesystem, 1K-blocks, Used, Available, Use%, Mounted on.