A long task on my own machine died halfway through with a write error, and only when I went looking did I run df and find the root SSD sitting at 100%. I'd been reacting to a crash that a five-line check would have warned me about an hour earlier. There was no heads-up, no yellow light — the disk went from "fine" to "out" while I wasn't looking, and the first I heard of it was a program falling over because there was nowhere left to write.
That's the nature of a full disk: it gives no warning on its own, and by the time something fails loudly enough to notice, you're already past the point where you had options. The fix isn't clever, which is the point — read the usage percentage off df, compare it to a threshold you choose, and say something while there's still room to act. Run it once and it tells you where you stand right now; run it on a schedule and it turns a silent wall you hit into a warning you get ahead of. The script below is the difference between finding out from a threshold and finding out from a crash. I'd rather get the warning than clean up after the wall.
What Does the Disk Space Warning Script Look Like?
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).
How Do I Set Up and Run the Disk Space Warning Script?
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:
How Do I Schedule This Script to Run Automatically?
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. Disk monitoring is one of several jobs every box needs from day one — the bash scripts every sysadmin needs guide covers the full baseline.
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.
What Are Common Variations of This Script?
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.
What Common Mistakes Break Disk Space Monitoring?
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. Once you identify the full partition, the find duplicate files script locates redundant copies consuming space with no extra tools.
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. For full SMTP setup, Gmail relay via msmtp, and send-once lockfiles that prevent inbox flooding, see the bash email alert script. Pair disk monitoring with CPU and RAM monitoring to cover all three resources from the same cron schedule.
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).
What Does Each Command in the Script Do?
| 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 to read usage and awk to pull out the Use% column: df / | awk 'NR==2 {print $5}' | tr -d '%'. That leaves a bare number you can compare against a threshold in an if test, which is exactly what the script above does before deciding whether to warn.
How do I get an alert when disk space is low?
The standard pattern is bash plus a threshold plus cron: the script compares current usage to a number you set, prints or emails a warning on breach, and cron runs it on a schedule so you never have to remember. For SMS or Slack instead of email, pipe the warning line into curl against a webhook rather than echo.
Why does my disk fill up without any warning?
Because nothing on a stock Linux box watches free space for you. A filesystem reports its usage when asked and stays silent otherwise, so usage climbs invisibly until a write fails. A threshold check is the thing that turns "the disk is full" into "the disk is getting full" while you still have room to act.
What does the df / command do in Linux?
df means disk free. It reports filesystem size, used space, available space, and the mount point. The / argument limits the output to the root filesystem. The columns are Filesystem, 1K-blocks, Used, Available, Use%, and Mounted on — the script reads Use% from the second line.
Part of the Server Monitoring · Disk Management collection