du Command
The du command (disk usage) displays directory and file disk usage in Linux and Unix systems. It shows the amount of disk space used by files and directories, making it essential for analyzing storage consumption and identifying large files or directories that may be consuming excessive space.
Syntax
Description
The du command recursively examines directories and reports the disk space used by each directory and its contents. Unlike df which shows filesystem-level usage, du provides detailed, file-level analysis to help identify what's consuming disk space.
Key features:
- Recursive directory analysis with size reporting
- Human-readable output formats (KB, MB, GB)
- Depth control to limit recursion levels
- Sorting and filtering capabilities
- Exclude patterns for specific files or directories
- Summary mode for quick directory totals
When to Use du vs df
- Use df: Check overall filesystem capacity and availability
- Use du: Find what files/directories are using space
- Workflow: df shows the problem, du helps find the cause
- Performance: df is faster, du can be slow on large directories
du vs df Comparison
du (Directory Usage)
- Shows file/directory level usage
- Recursive analysis
- Helps find space hogs
- Can be slow on large directories
- Detailed breakdown
df (Disk Free)
- Shows filesystem level usage
- Quick overview
- Shows available space
- Fast execution
- System-wide perspective
Sample du -h Output
4.0K ./config 120M ./logs 2.5G ./data/images 500M ./data/documents 3.0G ./data 45M ./cache 3.2G .
Examples
Basic directory analysis
du -h # Human-readable format
du -sh * # Summary of each item in current directory
du -sh /home/user # Total size of specific directory
Analyze directory sizes in different formats and detail levels
Find largest directories
du -h | sort -hr | head -10 # Top 10 largest directories
du -sh * | sort -hr # Sort top-level directories only
du -ah | sort -hr | head -20 # Include files, top 20 largest
Identify the largest consumers of disk space
Control recursion depth
du -h --max-depth=2 # Go 2 levels deep
du -h -d 1 /var # Alternative syntax (BSD style)
du -sh */ # Summary of subdirectories only
Limit how deep du searches to focus on specific levels
Include files and exclude patterns
du -h --exclude="*.log" # Exclude log files
du -h --exclude="*.tmp" --exclude="cache" .
du -h --exclude-from=exclude.txt # Exclude patterns from file
Customize what files and directories to include in analysis
Cross-filesystem and totals
du -ch dir1 dir2 dir3 # Show total for multiple directories
du -sh /home/* | awk '{sum+=$1} END {print sum}' # Sum all sizes
Handle multiple filesystems and calculate totals
Practical disk cleanup workflows
du -h | awk '$1 ~ /[0-9]+G/ {print $0}'
# Find largest files in current directory
du -ah | sort -hr | head -20
# Check what's using space in /var
sudo du -sh /var/* | sort -hr
# Monitor directory growth
watch -n 60 'du -sh /var/log'
Common workflows for disk space analysis and cleanup
⚡ Performance Tips
- Use --max-depth: Limit recursion for faster results
- Start specific: Target suspected directories instead of root
- Use -x flag: Stay on one filesystem to avoid slow network mounts
- Exclude unnecessary files: Skip temp files and caches
- Run during low activity: du can be I/O intensive
- Consider ncdu: Interactive alternative for large directories
💡 Tips and Best Practices
- Combine with sort: Always sort output to find largest items first
- Use -s for overview: Get quick summary without details
- Check permissions: Run with sudo for system directories
- Exclude mount points: Use -x to avoid crossing filesystems
- Save results: Large du runs can take time, save output
- Use with find: Combine for more complex file searches
Common Use Cases
- Quick directory overview:
du -sh * - Find space hogs:
du -h | sort -hr | head -10 - Check user directories:
sudo du -sh /home/* - Analyze system logs:
sudo du -sh /var/log/* - Before cleanup:
du -h --max-depth=2 | sort -hr - Monitor growth:
du -sh /var/log > usage_$(date +%Y%m%d).txt
Troubleshooting
- Permission denied: Use sudo for system directories
- Very slow execution: Use --max-depth or target specific directories
- Different results than df: du shows actual usage, df shows filesystem allocation
- Crossing filesystems: Use -x to stay on one filesystem
- Symbolic links: du follows symlinks by default, use -P to avoid