1. Display Disk Usage in Human-Readable Format
$ du -h
Shows disk usage for the current directory and all subdirectories in human-readable format (KB, MB, GB, etc.). The -h flag converts raw byte counts into easily understandable units.
Output format: 4.5M, 128K, 2.3G instead of raw byte counts.
2. Summary of Directory Total Only
$ du -sh /var/log
Displays only the total size of the specified directory without showing individual subdirectories. The -s flag means "summarize" and -h provides human-readable output.
Perfect for: Quick answer to "how big is this directory?" without all the subdirectory details.
3. Show Sizes for All Items in Directory
$ du -h --max-depth=1
Lists disk usage for the current directory and immediate subdirectories only (one level deep). This prevents deep recursion and gives you a clear first-level overview.
Depth control: --max-depth=1 shows only direct children, =2 shows children and grandchildren, etc.
-d 1 as a shorter alternative to --max-depth=1
4. Sort Output by Size (Largest First)
$ du -h | sort -hr
Combines du with sort to display directories by size, largest first. The -h flag on sort enables "human numeric sort" that correctly orders units (KB, MB, GB).
Sort flags: -h (human-readable), -r (reverse/descending order)
du -k | sort -rn
5. Show Top 10 Largest Directories
$ du -h | sort -hr | head -10
Chains du, sort, and head to display only the 10 largest directories. This is one of the most useful disk space troubleshooting commands.
Pipeline breakdown: du calculates sizes → sort orders by size → head shows top 10
alias ducks='du -h | sort -hr | head -10' (ducks = DU Check Sorted)
6. Exclude Specific Directories
$ du -h --exclude='*.log' --exclude=node_modules
Calculates disk usage while excluding specific patterns or directories. Multiple --exclude flags can be used to skip various paths.
Pattern matching: Supports wildcards (*) and can exclude by filename pattern or directory name.
7. Show Disk Usage with Grand Total
$ du -ch /home/* | tail -1
The -c flag adds a grand total line at the end. Combined with tail, this shows just the total disk usage across multiple directories.
Useful with wildcards: Calculate total size of all items matching a pattern.
8. Show Disk Usage Including Individual Files
$ du -ah /var/log | head -20
The -a flag shows disk usage for all files, not just directories. Combined with -h for readability, this gives complete file-level detail.
Detail level: Every single file and directory is listed with its size.
du -ah | sort -hr | head -20
9. Check Disk Usage with Threshold Filter
$ du -h --threshold=100M
Only displays directories and files larger than the specified threshold. This filters out small items and focuses on significant space consumers.
Threshold units: K (kilobytes), M (megabytes), G (gigabytes), T (terabytes)
--threshold=-1M shows items under 1MB
10. Show Apparent Size vs Actual Disk Usage
$ du -h --apparent-size
Shows file sizes rather than disk usage. Without this flag, du shows actual disk space used (including filesystem overhead). With it, you see the file's apparent size.
The difference: A 100-byte file on a filesystem with 4KB blocks uses 4KB on disk but has apparent size of 100 bytes.
Quick Reference: Common du Options
| Option | Description |
|---|---|
-h |
Human-readable output (KB, MB, GB) |
-s |
Summary only (total for directory) |
-a |
Show all files, not just directories |
-c |
Display grand total at the end |
-d N or --max-depth=N |
Limit recursion to N levels deep |
--exclude=PATTERN |
Exclude files matching pattern |
--threshold=SIZE |
Only show items larger than SIZE |
--apparent-size |
Show apparent size instead of disk usage |
-0 |
End lines with null character (for xargs) |
-x |
Stay on same filesystem (don't cross mount points) |
-L |
Follow symbolic links |
- Find largest directories in /var:
sudo du -h /var --max-depth=2 | sort -hr | head -20 - Check all user home directories:
sudo du -sh /home/* - Find directories over 1GB:
du -h --threshold=1G - Exclude hidden files:
du -h --exclude='.*' - Monitor directory size growth:
watch -n 60 'du -sh /var/log'
#!/bin/bash
# Save as disk-hogs.sh
echo "Top 10 Disk Space Consumers:"
du -h /home /var /opt --max-depth=2 2>/dev/null | sort -hr | head -10
du -hx / won't include mounted filesystems.