du — Estimate File Space Usage¶
Overview¶
du (disk usage) measures and reports the disk space occupied by files and directory trees. It is essential for identifying which directories or log files are consuming server disk space.
Command type: External (GNU coreutils)
Location: /usr/bin/du
Standard: POSIX.1-2017
Syntax¶
Options Reference¶
| Option | Description |
|---|---|
-h | --human-readable — Print sizes in human-readable units (K, M, G) |
-s | --summarize — Display only a total size for each argument |
-d N | --max-depth=N — Print total size for directories only down to depth N |
-a | --all |
-c | --total |
-x | --one-file-system |
--exclude=PATTERN | Exclude files matching PATTERN |
Examples¶
Check Total Size of Current Directory¶
Find Top 10 Largest Directories in /var¶
Check Depth 1 Sizes in /var/log¶
Sample output:
Exclude Specific Patterns¶
# Exclude node_modules and .git folders
du -h --max-depth=1 --exclude="node_modules" --exclude=".git" /projects
Interview Questions¶
How do you find the 10 largest subdirectories under /var using du?
-d 1 restricts depth to 1 level, sort -hr sorts human-readable numbers in reverse (largest first), and head -10 outputs the top 10. Related Commands¶
| Command | Description |
|---|---|
df | Report filesystem disk space |
ncdu | NCurses interactive disk usage analyzer |
find | Search for files by size (find . -size +100M) |