Disk Usage Analysis with du Command
Overview
The du command (disk usage) is a fundamental Unix/Linux utility that displays the disk space used by files and directories. It recursively examines directory trees and reports space consumption, making it an essential tool for system administrators, developers, and users who need to monitor and manage disk space effectively.
Basic Syntax
`bash
du [OPTIONS] [FILE/DIRECTORY]
`
Core Functionality
The du command calculates and displays the disk usage of files and directories by examining their actual size on disk. Unlike the ls command which shows file sizes, du provides information about how much disk space is actually consumed, including the space used by subdirectories.
Essential Options and Flags
| Option | Long Form | Description | Example |
|--------|-----------|-------------|---------|
| -h | --human-readable | Display sizes in human readable format (K, M, G) | du -h /home |
| -s | --summarize | Display only total for each argument | du -s /var/log |
| -a | --all | Show sizes for all files, not just directories | du -a /etc |
| -c | --total | Produce a grand total | du -c /home/* |
| -d N | --max-depth=N | Limit recursion to N levels | du -d 2 /usr |
| -x | --one-file-system | Skip directories on different file systems | du -x / |
| -L | --dereference | Follow symbolic links | du -L /usr/bin |
| -S | --separate-dirs | Don't include subdirectory sizes | du -S /home |
| -k | --kilobytes | Display sizes in kilobytes | du -k /tmp |
| -m | --megabytes | Display sizes in megabytes | du -m /var |
Advanced Options
| Option | Long Form | Description | Use Case |
|--------|-----------|-------------|----------|
| --apparent-size | --apparent-size | Show apparent sizes rather than disk usage | Comparing file sizes vs disk usage |
| --exclude=PATTERN | --exclude=PATTERN | Exclude files matching PATTERN | du --exclude="*.log" /var |
| --exclude-from=FILE | --exclude-from=FILE | Exclude files listed in FILE | Bulk exclusions from file |
| --time | --time | Show time of last modification | du --time /home/user |
| --time-style=STYLE | --time-style=STYLE | Show times using given style | du --time --time-style=iso |
| -0 | --null | End each output line with NULL | Scripting with special filenames |
Basic Usage Examples
Simple Directory Analysis
`bash
Check current directory usage
duCheck specific directory
du /home/usernameHuman-readable format
du -h /var/log`Output Example:
`
4.0K /var/log/apt
12K /var/log/journal/a1b2c3d4e5f6
16K /var/log/journal
156K /var/log
`
Summarized Usage
`bash
Get total size only
du -sh /home/username`Output Example:
`
2.3G /home/username
`
All Files and Directories
`bash
Show all files, not just directories
du -ah /etc/nginx`Output Example:
`
4.0K /etc/nginx/nginx.conf
2.0K /etc/nginx/mime.types
1.0K /etc/nginx/fastcgi_params
8.0K /etc/nginx
`
Practical Use Cases
1. Finding Large Directories
`bash
Find largest directories in /home
du -h /home | sort -hr | head -10Alternative with depth limit
du -h -d 1 /home | sort -hr`Notes:
- sort -hr sorts in human-readable format in reverse order
- head -10 shows top 10 results
- -d 1 limits depth to prevent deep recursion
2. Monitoring System Directories
`bash
Check system log usage
du -sh /var/log/*Monitor temporary files
du -sh /tmp /var/tmpCheck package cache
du -sh /var/cache/apt`Example Output:
`
45M /var/log/journal
12M /var/log/syslog
8.5M /var/log/kern.log
2.1M /var/log/auth.log
`
3. Analyzing User Home Directories
`bash
Check all user directories
sudo du -sh /home/*Detailed analysis of specific user
du -h /home/username | sort -hr | head -20`Advanced Usage Scenarios
Excluding Files and Patterns
`bash
Exclude log files
du -h --exclude="*.log" /varExclude multiple patterns
du -h --exclude=".log" --exclude=".tmp" /home/userExclude from file
echo "*.log" > /tmp/exclude_patterns echo "*.cache" >> /tmp/exclude_patterns du -h --exclude-from=/tmp/exclude_patterns /home`Cross-Filesystem Analysis
`bash
Stay within single filesystem
du -x -h /Compare with cross-filesystem
du -h /`Note: The -x option prevents du from crossing filesystem boundaries, useful when you have multiple mounted filesystems.
Time-based Analysis
`bash
Show modification times
du -h --time /home/user/DocumentsSpecific time format
du -h --time --time-style=long-iso /var/log`Output Example:
`
4.0K 2023-12-15 14:30 /var/log/apt
12K 2023-12-15 15:45 /var/log/alternatives.log
`
Combining with Other Commands
Finding Largest Files
`bash
Combine with find for large files
find /home -type f -exec du -h {} + | sort -hr | head -20Files larger than 100MB
find /home -type f -size +100M -exec du -h {} +`Real-time Monitoring
`bash
Watch directory growth
watch -n 5 'du -sh /var/log'Monitor multiple directories
watch -n 10 'du -sh /home /var /tmp'`Scripting Examples
`bash
#!/bin/bash
Disk usage report script
echo "=== Disk Usage Report ===" echo "Generated: $(date)" echo
echo "Top 10 largest directories in /home:" du -h /home | sort -hr | head -10 echo
echo "System directory usage:"
for dir in /var/log /var/cache /tmp /var/tmp; do
if [ -d "$dir" ]; then
echo "$(du -sh "$dir" | cut -f1): $dir"
fi
done
`
Performance Considerations
Large Directory Trees
`bash
For very large directories, use depth limits
du -h -d 2 /usrOr exclude problematic subdirectories
du -h --exclude="proc" --exclude="sys" /`Network Filesystems
`bash
Be cautious with network mounts
du -x -h / # Excludes network mountsSpecific network filesystem analysis
du -h /mnt/nfs_share`Output Formatting and Parsing
Machine-Readable Output
`bash
Null-separated output for scripts
du -0 -a /home | while IFS= read -r -d '' line; do echo "Processing: $line" doneBytes only (no units)
du -b /home/user/file.txt`Custom Formatting
`bash
Kilobytes with totals
du -k -c /home/*Apparent size vs disk usage comparison
echo "Apparent size:" du --apparent-size -h /home/user echo "Disk usage:" du -h /home/user`Troubleshooting Common Issues
Permission Denied Errors
`bash
Run with appropriate privileges
sudo du -sh /rootRedirect errors to suppress them
du -sh /home 2>/dev/nullShow errors separately
du -sh /home 2>&1 | grep "Permission denied"`Symbolic Links
`bash
Default behavior (don't follow symlinks)
du -sh /usr/binFollow symbolic links
du -L -sh /usr/binShow difference
echo "Without following symlinks:" du -sh /usr/bin echo "Following symlinks:" du -L -sh /usr/bin`Sparse Files
`bash
Show apparent size (logical size)
du --apparent-size -h sparse_file.imgShow actual disk usage
du -h sparse_file.img`Comparison Table: du vs Other Tools
| Tool | Purpose | Output | Speed | Detail Level |
|------|---------|--------|-------|--------------|
| du | Directory/file disk usage | Hierarchical listing | Moderate | High |
| df | Filesystem usage | Summary by filesystem | Fast | Low |
| ls -l | File listing with sizes | File-by-file | Fast | Medium |
| ncdu | Interactive disk usage | Curses interface | Slow | Very High |
| tree | Directory structure | Tree format | Fast | Structure only |
Environment Variables
| Variable | Description | Example |
|----------|-------------|---------|
| DU_BLOCK_SIZE | Default block size | export DU_BLOCK_SIZE=1024 |
| POSIXLY_CORRECT | POSIX compliance mode | export POSIXLY_CORRECT=1 |
| TIME_STYLE | Default time format | export TIME_STYLE=iso |
Best Practices
Regular Monitoring
`bash
Create monitoring alias
alias diskusage='du -h -d 1 / | sort -hr | head -20'Cron job for regular reports
0 6 0 du -sh /home/* | mail -s "Weekly Disk Usage" admin@domain.com`Efficient Usage
`bash
Use depth limits for large filesystems
du -h -d 3 /usrCombine with other tools effectively
du -sh */ | sort -hr # Current directory subdirs only`System Administration
`bash
Check for rapid growth
du -sh /var/log && sleep 3600 && du -sh /var/logAutomated cleanup trigger
USAGE=$(du -sm /tmp | cut -f1) if [ $USAGE -gt 1000 ]; then echo "Temporary directory exceeds 1GB" # Trigger cleanup fi`Integration with System Monitoring
Log Analysis
`bash
Monitor log growth
du -sh /var/log/* | awk '$1 ~ /G/ { print "Large log: " $0 }'Historical comparison
du -sh /var/log > /tmp/log_usage_$(date +%Y%m%d)`Backup Planning
`bash
Calculate backup sizes
du -sh /home /etc /var/www > backup_sizes.txtEstimate backup time (assuming 50MB/s)
TOTAL_KB=$(du -sk /home | cut -f1) TOTAL_MB=$((TOTAL_KB / 1024)) BACKUP_TIME=$((TOTAL_MB / 50)) echo "Estimated backup time: ${BACKUP_TIME} minutes"`Security Considerations
Sensitive Directory Analysis
`bash
Careful with sensitive directories
sudo du -sh /etc/shadow 2>/dev/null || echo "Cannot access shadow file"Audit user space usage
sudo du -sh /home/* | while read size dir; do echo "User $(basename $dir): $size" done`Access Control
`bash
Check accessible directories only
find /home -type d -readable -exec du -sh {} + 2>/dev/null`The du command is an indispensable tool for disk space management and system administration. Its flexibility in output formatting, depth control, and integration capabilities make it suitable for both interactive use and automated scripting. Understanding its various options and use cases enables efficient disk space monitoring and management across different scenarios, from personal file organization to enterprise system administration.