How to Monitor Disk Usage by Directory on Linux Server - Complete Storage Management Guide
Are you wondering how to check which directories are consuming the most disk space on your Linux server? Need to identify space-consuming locations before your server runs out of storage? This comprehensive guide shows you multiple metho...
How to Monitor Disk Usage by Directory on Linux Server - Complete Storage Management Guide
Are you wondering how to check which directories are consuming the most disk space on your Linux server? Need to identify space-consuming locations before your server runs out of storage? This comprehensive guide shows you multiple methods to monitor disk usage by directory, track storage growth over time, identify large files and folders, and prevent disk space issues that can crash your Linux server.
Why Monitoring Directory Disk Usage Matters
Disk space management is critical for Linux server stability. When disk space runs out, services can fail, databases can crash, and your entire server can become unresponsive. Learning how to monitor disk usage by directory helps you identify storage bottlenecks early, plan cleanup operations proactively, and maintain optimal server performance. Regular directory disk usage monitoring prevents costly downtime incidents and helps you optimize storage allocation across your Linux infrastructure.
Method 1: Check Disk Usage with du Command
The du (disk usage) command is the most common tool for checking directory disk usage on Linux servers. This built-in utility shows you exactly how much space each directory consumes, helping you identify where your disk space is being used.
Basic Directory Disk Usage Check
To check disk usage for a specific directory:
# Check disk usage for current directory
du -h .
# Check disk usage for specific directory (human-readable format)
du -h /var/log
# Check disk usage summary for a directory
du -sh /path/to/directory
The -h flag displays sizes in human-readable format (KB, MB, GB), while -s shows only a summary total for the specified directory.
Find Largest Directories
To identify the largest directories consuming disk space:
# Top 10 largest directories in root filesystem
du -h --max-depth=1 / | sort -rh | head -10
# Top 20 largest directories in /var
du -h --max-depth=1 /var | sort -rh | head -20
# Find largest directories excluding specific paths
du -h --max-depth=1 / | grep -v '/proc\|/sys\|/dev' | sort -rh | head -10
The --max-depth=1 option limits the search to one level deep, sort -rh sorts by size in reverse order, and head -10 shows only the top 10 results.
Check Disk Usage for Multiple Directories
To monitor disk usage across multiple directories simultaneously:
# Check multiple directories at once
du -sh /var/log /var/www /home /tmp
# Check all top-level directories
du -sh /* 2>/dev/null | sort -rh
# Check specific user directories
du -sh /home/* | sort -rh
Method 2: Advanced Disk Usage Analysis Techniques
Beyond basic directory checks, you can use advanced techniques to analyze disk usage patterns and identify storage issues.
Find Directories Larger Than Threshold
To find directories exceeding a specific size:
# Find directories larger than 1GB
du -h --max-depth=1 / | awk '$1 ~ /G/ {print}'
# Find directories larger than 100MB
du -h --max-depth=1 /var | awk '$1 ~ /[0-9]+M/ {print}' | sort -rh
# Find directories using more than 10GB
du -h --max-depth=1 / | grep -E '[0-9]+G' | sort -rh
Monitor Directory Growth Over Time
To track how directory sizes change over time:
# Save current directory sizes
du -h --max-depth=1 /var > /tmp/var-usage-$(date +%Y%m%d).txt
# Compare with previous snapshot
diff /tmp/var-usage-old.txt /tmp/var-usage-new.txt
# Calculate growth rate
du -sh /var/log && sleep 3600 && du -sh /var/log
Exclude Specific Directories from Analysis
When checking disk usage, you may want to exclude certain directories:
# Check disk usage excluding /proc, /sys, /dev
du -h --exclude=/proc --exclude=/sys --exclude=/dev --max-depth=1 / | sort -rh | head -10
# Check /var excluding log directories
du -h --exclude=/var/log --max-depth=1 /var | sort -rh
Method 3: Automated Directory Disk Usage Monitoring with Zuzia.app
Manually checking directory disk usage works for occasional audits, but for production Linux servers, you need automated monitoring that alerts you when directories grow too large or consume excessive disk space. Zuzia.app provides comprehensive directory disk usage monitoring through scheduled command execution.
Setting Up Automated Directory Monitoring
-
Add Scheduled Task in Zuzia.app Dashboard
- Navigate to your server in Zuzia.app
- Click "Add Scheduled Task"
- Choose "Command Execution" as the task type
-
Configure Disk Usage Check Command
- Enter command:
du -h --max-depth=1 /var | sort -rh | head -10 - Set execution frequency: Once daily or every few hours for critical directories
- Configure alert conditions: Alert when directory sizes exceed thresholds
- Set up comparison with previous runs to detect growth
- Enter command:
-
Set Up Notifications
- Choose notification channels (email, webhook, Slack, etc.)
- Configure alert thresholds (e.g., alert if /var/log exceeds 10GB)
- Set up escalation rules for critical directories
- Configure different thresholds for different directory types
Monitor Critical Directories
For mission-critical directories, create dedicated monitoring tasks:
# Monitor /var/log directory
du -sh /var/log
# Monitor web server directories
du -sh /var/www /usr/share/nginx
# Monitor database directories
du -sh /var/lib/mysql /var/lib/postgresql
# Monitor user home directories
du -sh /home/* | sort -rh | head -10
Zuzia.app stores all command outputs in its database, allowing you to track directory growth over time, identify patterns in storage usage, and detect anomalies before they cause disk space issues.
Method 4: Identify Large Files Within Directories
Once you've identified directories consuming disk space, you need to find the specific large files within those directories.
Find Largest Files in Directory
To identify the largest files in a specific directory:
# Find largest files in current directory
find . -type f -exec du -h {} + | sort -rh | head -20
# Find largest files in /var/log
find /var/log -type f -exec du -h {} + | sort -rh | head -20
# Find files larger than 100MB
find /var -type f -size +100M -exec du -h {} + | sort -rh
Find Files by Type Consuming Space
To identify which file types are consuming the most space:
# Find largest log files
find /var/log -name "*.log" -exec du -h {} + | sort -rh | head -10
# Find largest database files
find /var/lib/mysql -name "*.ibd" -exec du -h {} + | sort -rh | head -10
# Find largest image files
find /var/www -type f \( -name "*.jpg" -o -name "*.png" \) -exec du -h {} + | sort -rh | head -20
Real-World Use Cases for Directory Disk Usage Monitoring
Web Server Storage Management
For web servers, monitor directories containing website files:
# Check web root directory sizes
du -sh /var/www/* | sort -rh
# Monitor upload directories
du -sh /var/www/*/uploads | sort -rh
# Check cache directories
du -sh /var/cache/* | sort -rh
Database Storage Monitoring
For database servers, track database directory growth:
# Monitor MySQL data directory
du -sh /var/lib/mysql/* | sort -rh
# Check PostgreSQL database sizes
du -sh /var/lib/postgresql/*/main/base | sort -rh
# Monitor database log directories
du -sh /var/log/mysql /var/log/postgresql | sort -rh
Log File Management
For log management, identify directories with excessive log files:
# Check log directory sizes
du -sh /var/log/* | sort -rh
# Find largest log files
find /var/log -type f -name "*.log" -exec du -h {} + | sort -rh | head -20
# Monitor application log directories
du -sh /var/log/nginx /var/log/apache2 /var/log/application | sort -rh
Best Practices for Directory Disk Usage Monitoring
1. Monitor Critical Directories Regularly
Check disk usage for critical directories (like /var, /home, /tmp) daily or every few hours. This allows you to detect growth patterns early and prevent disk space issues. Use Zuzia.app automated monitoring to check directory sizes continuously without manual intervention.
2. Set Appropriate Thresholds
Configure different alert thresholds for different directory types. For example, /var/log might have a 20GB threshold, while /home directories might have a 100GB threshold per user. Adjust thresholds based on your server's storage capacity and usage patterns.
3. Track Growth Trends
Monitor directory growth over time to identify trends. Rapidly growing directories might indicate log rotation issues, database bloat, or application problems. Use Zuzia.app's historical data to track growth rates and predict when directories will exceed capacity.
4. Automate Cleanup Operations
Combine directory monitoring with automated cleanup scripts. When directories exceed thresholds, automatically run cleanup operations like log rotation, temporary file removal, or old backup deletion. Always test cleanup scripts in non-production environments first.
5. Document Directory Purposes
Maintain documentation about what each directory contains and why it exists. This helps you make informed decisions about cleanup operations and prevents accidental deletion of important data.
Troubleshooting Common Directory Disk Usage Issues
Directory Growing Too Fast
If a directory is growing unexpectedly fast:
# Identify what's causing growth
du -h --max-depth=2 /problematic/directory | sort -rh | head -20
# Check for large files recently created
find /problematic/directory -type f -mtime -1 -exec du -h {} + | sort -rh
# Monitor directory in real-time
watch -n 60 'du -sh /problematic/directory'
Cannot Determine Directory Size
If du commands are slow or hanging:
# Use timeout to prevent hanging
timeout 30 du -h --max-depth=1 /slow/directory
# Check for filesystem issues
df -h /slow/directory
# Use ncdu for interactive browsing (if available)
ncdu /slow/directory
Disk Usage Not Matching df Output
If directory sizes don't match filesystem usage:
# Check for deleted files still in use
lsof +L1
# Check for mount points
df -h
mount | grep /problematic/directory
# Verify you're checking the correct filesystem
stat /problematic/directory
FAQ: Common Questions About Monitoring Directory Disk Usage
How often should I check directory disk usage on my Linux server?
We recommend checking directory disk usage once daily for most directories, and every few hours for critical directories like /var/log or database directories. This allows you to identify growth patterns without excessive system load. Use Zuzia.app automated monitoring to check directory sizes continuously without manual intervention.
What should I do if a directory is consuming too much disk space?
When a directory exceeds your threshold, first identify what's consuming space using find commands to locate large files. Then investigate whether the files are necessary (logs, backups, cache) or can be safely removed. Set up automated cleanup scripts for temporary files and implement log rotation for log directories. Always verify files before deletion in production environments.
Can I monitor disk usage for multiple directories simultaneously?
Yes, you can check multiple directories at once using commands like du -sh /var/log /var/www /home /tmp or by creating separate monitoring tasks in Zuzia.app for each critical directory. Zuzia.app allows you to monitor multiple directories across multiple servers from a single dashboard, making it easy to track storage usage across your entire infrastructure.
How do I find which files are consuming the most space within a directory?
Use the find command combined with du to identify large files: find /directory -type f -exec du -h {} + | sort -rh | head -20. This shows the 20 largest files in the directory. You can also filter by file type, modification date, or size threshold to narrow down your search.
What's the difference between du and df commands for checking disk usage?
The du command shows disk usage of files and directories, helping you identify which directories consume space. The df command shows filesystem-level disk usage, displaying how much space is available on each mounted filesystem. Use du to find space-consuming directories and df to check overall filesystem capacity.
How can I prevent directories from growing too large automatically?
Set up automated monitoring in Zuzia.app with alert thresholds, then combine it with cleanup scripts that run when thresholds are exceeded. Implement log rotation for log directories, set up automatic deletion of old temporary files, and configure database maintenance tasks to prevent bloat. Always test automation scripts thoroughly before deploying to production.
Does Zuzia.app track directory growth trends over time?
Yes, Zuzia.app stores all command outputs in its database, allowing you to track directory sizes over time and identify growth patterns. You can view historical data to see how directory sizes change, identify trends, and predict when directories will exceed capacity. This helps you plan storage upgrades and cleanup operations proactively.