In theory, checking disk usage looks simple — just grab the percentage from df -h. But in the real world, scripts break, formats differ, and human‑readable values like 374G don’t compare cleanly. This post is about the lessons learned when theory meets reality.
Disk Usage Monitoring in Linux: Percentage vs. Actual Size
Monitoring disk usage is one of the most common tasks for system administrators and developers. But there’s often confusion between checking percentage usage (df -h) and checking actual disk space (du -sh with numfmt). Let’s break down the challenges, solutions, pros, and cons of each approach.
❓ Common Questions
Should I monitor disk usage by percentage or by actual size?
Why does my script fail with “integer expression expected” errors?
How can I compare human‑readable sizes like 192K, 374G, or 2T against thresholds?
Which method is more reliable across different environments (Linux, Git Bash, macOS)?
⚡ The Challenge
Using df -h with percentages
A typical script might look like this:
disk_usage=$(df -h | awk 'NR>1 {print $5}' | sed 's/%//')
for usage in $disk_usage; do
if [ "$usage" -gt 70 ]; then
echo "Warning: Disk usage is high ($usage%)"
else
echo "Disk usage is normal ($usage%)"
fi
done
Problem:
Sometimes df -h outputs values like 374G in other columns.
If parsing isn’t precise, your script may grab 374G instead of 22%.
[ -gt ] only works with integers, so 374G causes integer expression expected errors.
Using du -sh with numfmt
A more robust approach is to normalize values into bytes:
size=$(du -sh | awk '{print $1}')
bytes=$(numfmt --from=iec "$size")
threshold=$(numfmt --from=iec 100G)
if [ "$bytes" -gt "$threshold" ]; then
echo "Disk usage is high: $size"
else
echo "Disk usage is normal: $size"
fi
Here:
du -sh → gives human‑readable size (192K, 374G, etc.).
numfmt --from=iec → converts those into raw integers (bytes).
Thresholds like 100G, 500M, 2T are also converted into bytes.
Comparisons are now reliable and portable.
✅ Solutions
For percentage checks: Use df -h | awk 'NR>1 {print $5}' | sed 's/%//' to extract only numeric percentages.
For actual size checks: Use du -sh + numfmt to convert human‑readable values into integers.
Hybrid approach: Monitor both percentage and actual size for a complete picture.
🚀 Conclusion
If you just want a quick warning when usage exceeds 70%, percentage checks with df -h are fine.
If you need robust monitoring across environments, or want to enforce thresholds like “alert me if usage exceeds 100G,” then numfmt is the best choice.
In real production scripts, combining both methods gives the most reliable monitoring.

Top comments (0)