The du (Disk Usage) Command
Last Updated: 2025-01-01
2 min read
Overview
The du (disk usage) command estimates and displays the disk space used by files and directories. It’s an essential tool for identifying what’s consuming storage on your hosting account.
Basic Syntax
du [options] [path]
If no path is specified, du reports on the current directory.
Common Usage Examples
Check the Size of a Specific Directory
du -sh /home/username/public_html
# Output: 1.2G /home/username/public_html
-s— Summary (total size only, don’t list subdirectories)-h— Human-readable sizes (K, M, G)
List Subdirectory Sizes
du -h --max-depth=1 /home/username/public_html
This shows the size of each immediate subdirectory — extremely useful for pinpointing where space is being used:
250M /home/username/public_html/wp-content
5.0M /home/username/public_html/wp-admin
12M /home/username/public_html/wp-includes
1.2G /home/username/public_html
Find the Largest Directories
Combine du with sort to rank directories by size:
du -h --max-depth=1 /home/username | sort -rh | head -20
sort -rh— Reverse sort by human-readable numbers (largest first)head -20— Show only the top 20 results
Check Size of All Files in a Directory
du -ah /home/username/public_html | sort -rh | head -20
The -a flag includes individual files, not just directories.
Useful Flags Reference
| Flag | Description |
|---|---|
-h | Human-readable output (KB, MB, GB) |
-s | Display only the total for each argument |
-a | Include individual files, not just directories |
--max-depth=N | Limit directory depth to N levels |
-c | Produce a grand total at the end |
--exclude=PATTERN | Skip files matching the pattern |
-k | Display sizes in kilobytes |
-m | Display sizes in megabytes |
Practical Examples for Hosting
Check your entire home directory usage
du -sh ~
Find large log files eating up space
du -ah /home/username/logs | sort -rh | head -10
Exclude certain directories from the count
du -sh --exclude='*.log' /home/username/public_html
Show total size of multiple directories
du -shc /home/username/public_html /home/username/mail
Tips
- Run
dufrom your home directory with--max-depth=1as a first step whenever you need to investigate disk usage. - Pair
duwithfindto locate specific large files (see the related article on thefindcommand). - If your hosting plan is running low on space, check for old backups, large log files, and unused media uploads first.
Tags:
ssh
linux
disk-usage
du