Monitoring directory growth is essential for tracking logs, temporary files, and catching runaway disk usage before it becomes a problem. Linux offers several approaches depending on whether you need simple periodic checks, interactive exploration, or event-driven monitoring.
Simple periodic monitoring
The watch command repeatedly runs a command at set intervals:
watch -n 1 du -sh /path/to/dir
This updates every 1 second (-n 1) and shows the total size in human-readable format. Press q to exit. The du command can be resource-intensive on large directories, so if you’re monitoring frequently, increase the interval:
watch -n 5 du -sh /var/log /tmp /home
This checks multiple directories every 5 seconds.
Interactive exploration with ncdu
For deeper analysis, ncdu (NCurses Disk Usage) is far superior to navigating with du manually. It builds an interactive tree view and lets you identify which subdirectories consume the most space:
ncdu /path/to/dir
Navigate with arrow keys, press d to delete files directly, and q to quit. The initial scan takes longer on large directories, but subsequent navigation is instant. Use -o to export results:
ncdu -o /tmp/scan.json /var/log
ncdu -f /tmp/scan.json
This lets you compare scans over time.
Event-driven monitoring
For production systems, trigger actions when files are added or removed:
inotifywait -m -e create,delete -r /var/log | while read path action file; do
size=$(du -sh /var/log | cut -f1)
echo "$(date): $action on $file, total: $size" >> /var/log/monitor.log
done
The -m flag keeps inotifywait running continuously, -e specifies events, and -r watches recursively. This is useful for alerting when log directories grow unexpectedly.
Filesystem-wide monitoring
To see overall usage across mounted filesystems:
df -h
For a sorted view of which filesystems are filling up fastest:
df -h | sort -k5 -rn
This sorts by percentage used (column 5) in reverse numeric order.
Monitoring with alerts
Combine du with a cron job to alert when a directory exceeds a threshold:
#!/bin/bash
dir="/var/log"
limit=$((10 * 1024 * 1024)) # 10GB in bytes
size=$(du -sb "$dir" | cut -f1)
if [ $size -gt $limit ]; then
echo "Warning: $dir is $(( size / 1024 / 1024 ))MB" | mail -s "Disk alert" admin@example.com
fi
Run this via cron:
0 * * * * /usr/local/bin/check-disk-usage.sh
This checks hourly and emails alerts when thresholds are exceeded.
Performance considerations
For very large directories (millions of files), du becomes slow. Use ncdu for interactive analysis or consider hardware-backed solutions like quotas. On production systems, avoid running du -s too frequently—cache results or increase watch intervals to every 30 seconds or more.
