du
Overview
The du (disk usage) command displays the amount of disk space used by files and directories. It’s essential for disk space management and finding large files or directories.
Syntax
du [options] [file/directory...]Common Options
| Option | Description |
|---|---|
-h |
Human-readable sizes (K, M, G) |
-s |
Summary only (total for each argument) |
-a |
Show all files, not just directories |
-c |
Display grand total |
-d depth |
Maximum depth to display |
-x |
Stay on same filesystem |
-L |
Follow symbolic links |
-P |
Don’t follow symbolic links |
-0 |
End lines with null character |
--max-depth=n |
Limit directory depth |
--exclude=pattern |
Exclude files matching pattern |
--time |
Show modification time |
Key Use Cases
- Find disk space usage
- Identify large directories
- Disk cleanup planning
- Storage analysis
- System monitoring
Examples with Explanations
Example 1: Current Directory Usage
du -hShows disk usage of current directory and subdirectories
Example 2: Summary Only
du -sh *Shows total size of each item in current directory
Example 3: Specific Directory
du -h /var/logShows disk usage of /var/log directory
Example 4: Top-level Summary
du -h --max-depth=1 /homeShows usage of immediate subdirectories only
Finding Large Files/Directories
Largest directories:
du -h | sort -hr | head -10Largest files and directories:
du -ah | sort -hr | head -20Directories over 1GB:
du -h | awk '$1 ~ /G/ {print}'
Common Usage Patterns
Quick size check:
du -sh directory_nameFind space hogs:
du -h --max-depth=2 / | sort -hr | head -20Exclude certain files:
du -h --exclude="*.log" /var
Advanced Usage
Show modification times:
du -h --time /home/userStay on filesystem:
du -hx /Include all files:
du -ah /etc | head -20
Performance Analysis
- Can be slow on large filesystems
- I/O intensive operation
- Memory usage is minimal
- Use –max-depth to limit scope
- Consider excluding network mounts
Best Practices
- Use human-readable format (-h)
- Limit depth for large directories
- Exclude temporary files when needed
- Use summary mode for quick checks
- Combine with sort for analysis
Disk Cleanup Strategies
Find old large files:
find /home -size +100M -mtime +30 -exec du -h {} \;Analyze log directories:
du -h /var/log/* | sort -hrCheck user directories:
du -sh /home/* | sort -hr
Scripting Applications
Disk usage monitoring:
#!/bin/bash THRESHOLD=80 USAGE=$(du -s /home | awk '{print $1}') TOTAL=$(df /home | awk 'NR==2 {print $2}') PERCENT=$((USAGE * 100 / TOTAL)) if [ $PERCENT -gt $THRESHOLD ]; then echo "Disk usage warning: ${PERCENT}%" fiCleanup automation:
cleanup_large_files() { du -ah /tmp | awk '$1 ~ /[0-9]+G/ {print $2}' | \ while read file; do echo "Large file found: $file" # Add cleanup logic done }
Integration Examples
With find for targeted analysis:
find /var -name "*.log" -exec du -h {} \; | sort -hrSystem health check:
echo "Top 10 largest directories:" du -h --max-depth=2 / 2>/dev/null | sort -hr | head -10User quota monitoring:
for user in $(ls /home); do echo "$user: $(du -sh /home/$user 2>/dev/null | cut -f1)" done
Output Formatting
Custom format with awk:
du -h | awk '{printf "%-10s %s\n", $1, $2}'CSV output:
du -sb * | awk '{printf "%s,%s\n", $1, $2}'JSON-like format:
du -sh * | awk '{printf "{\"size\":\"%s\",\"path\":\"%s\"}\n", $1, $2}'
Troubleshooting
- Permission denied errors
- Slow performance on large directories
- Network filesystem timeouts
- Symbolic link loops
- Filesystem crossing issues
Security Considerations
- May reveal directory structure
- Can be resource intensive
- Consider access permissions
- Monitor for unusual disk usage
- Protect sensitive directory information