du Command Guide
The du (disk usage) command estimates file and directory space usage. Learn how to find what's consuming your disk space.
5 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
duDirectory usagedu -hHuman readabledu -sSummary totalOptions
-d 1Depth level 1-cShow grand total-aInclude filesCommon
du -sh *Size of each itemdu -sh * | sort -hSorted by sizedu -sh . --exclude=dirExclude dirDownloadable Image Preview
Failed to generate preview
Basic Usage
du shows disk usage of files and directories.
bash
# Current directory
du
# Specific directory
du /path/to/directory
# Specific file
du file.txtCommon Options
du Options
| -h | Human-readable sizes (K, M, G) |
| -s | Summary only (total size) |
| -a | Include files, not just directories |
| -c | Show grand total |
| -d N | Limit depth to N levels |
| --max-depth=N | Same as -d N |
| -x | Stay on one filesystem |
| --exclude | Exclude matching files |
Human-Readable Output
bash
# Human-readable sizes
du -h
# Summary with human-readable
du -sh directory/
# All files and directories
du -ahSummary and Totals
bash
# Total size of directory
du -sh /home/user
# Total of multiple directories
du -sh dir1 dir2 dir3
# With grand total
du -ch dir1 dir2 dir3Limiting Depth
bash
# One level deep
du -h -d 1
# Two levels deep
du -h --max-depth=2
# Just immediate subdirectories
du -sh */Practical Examples
Find largest directories
bash
du -h -d 1 | sort -hr | head -10Find largest files
bash
du -ah | sort -hr | head -20Check home directory usage
bash
du -sh ~/*Find space hogs in root
bash
sudo du -h -d 1 / 2>/dev/null | sort -hr | head -10Exclude certain directories
bash
du -h --exclude="node_modules" --exclude=".git" -d 1Compare directory sizes
bash
du -sh project_v1 project_v2Find directories over 1GB
bash
du -h -d 2 | grep -E '^[0-9.]+G'Tip
Use
-x to stay on one filesystem and avoid crossing mount points when checking root directories.du vs df
du and df serve different purposes:
Comparison
| du | Disk Usage - size of files/directories |
| df | Disk Free - filesystem space availability |
bash
# How much space does /home use?
du -sh /home
# How much space is free on the filesystem?
df -h /homeInteractive Tools
For interactive exploration, consider these tools:
bash
# ncdu - NCurses Disk Usage
sudo apt install ncdu
ncdu /
# dust - Modern du alternative (Rust)
dust
# duf - Modern df alternative
dufInfo
ncdu provides an interactive interface that makes it easy to navigate and find large directories.Apparent Size vs Disk Usage
bash
# Disk usage (actual blocks used)
du -sh file.txt
# Apparent size (logical size)
du -sh --apparent-size file.txtSparse files and filesystem block sizes can cause these to differ.
Summary
du is essential for managing disk space. Key takeaways:
- Use
du -shfor directory summary - Use
du -h -d 1for one level deep - Use
du | sort -hrto find largest items - Use
--excludeto skip directories - Consider
ncdufor interactive exploration