du
Show disk usage of files and directories
By CMD Script Team · 4 min read · Last updated
du [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-h | Print sizes in human-readable units (K, M, G) instead of raw block counts |
-s | Summarize: show only the total for each argument instead of every subdirectory |
-a | Include individual files in the output, not just directories |
-c | Print a grand total after all arguments |
-d N / --max-depth=N | Limit the depth of subdirectories reported (0 behaves like -s) |
-x | Stay on one filesystem; don't cross into other mounted filesystems |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (BSD du, some GNU long options unavailable)
What it does
du (disk usage) reports how much disk space specific files and directories are
actually consuming, by walking the directory tree and summing block usage. Unlike df,
which reports usage for an entire mounted filesystem, du answers "which of these
directories or files is actually using the space," making it the standard tool for
hunting down what's filling up a disk.
Beginner examples
du /path— recursively list the size of every subdirectory under/pathdu -h /path— same, with human-readable sizes like240Mor1.2Gdu -sh /path— collapse to a single total for/pathdu -ah /path— include individual files, not just directoriesdu -sh *— total size of each item in the current directory
du -sh /var/log
Advanced examples
- Find the largest subdirectories one level deep:
du -h --max-depth=1 /home | sort -h - Sort output so the largest items appear last (easy to spot at the bottom of a
terminal):
du -h /var | sort -h - Find the largest individual files under a path:
du -ah /var/log | sort -rh | head -20 - Get a grand total across several paths:
du -ch /var /home /tmp | tail -1 - Stay within one filesystem, ignoring mounted network shares or other filesystems:
du -sxh /
du -h --max-depth=1 / 2>/dev/null | sort -h
Common mistakes
- Running
du -h /pathwithout-son a huge tree and being overwhelmed by per-subdirectory output when only the total was wanted — add-sto summarize. - Comparing
duoutput directly todfoutput and expecting them to match exactly — they measure different things and can legitimately disagree. - Forgetting
-awhen trying to find individual large files — by defaultduonly reports directory totals, not per-file sizes. - Sorting
du -houtput with plainsortinstead ofsort -h, which sorts alphabetically rather than by the human-readable size (1Kwould sort before500Mincorrectly without-h). - Not accounting for permission errors when scanning system-wide (
du -sh /) as a non-root user — redirect stderr with2>/dev/nullto suppress noisy "Permission denied" lines.
Tips
- Combine
du -h --max-depth=1withsort -hto quickly narrow down which top-level subdirectory is consuming the most space, then repeat one level deeper. - Use
-cto get a combined grand total when passing multiple paths at once. - Use
-xwhen you want disk usage confined to the current filesystem, e.g. to avoidduwandering into a large mounted network share. du -sh .[!.]* * 2>/dev/nullis a handy pattern to include hidden files/directories alongside regular ones when summarizing the current directory.
Best practices
- Use
du -shfor quick totals anddu -h --max-depth=N | sort -hfor drilling down — don't try to eyeball unsorted recursive output on large trees. - When freeing up disk space, always cross-check
dufindings withdf -hafterward to confirm the space was actually reclaimed at the filesystem level. - Exclude filesystems you don't intend to scan with
-xto avoid slow, unintentional scans of network mounts. - In scripts, prefer
du -shon specific known paths over an unscoped scan of/, both for speed and to avoid permission-denied noise.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Hunting down what's filling up
/varwhen a server reports low disk space:du -h --max-depth=1 /var | sort -h. - Auditing user home directories for the biggest space consumers before enforcing quotas.
- Checking how large a build artifact or Docker layer directory has grown:
du -sh node_modules. - Comparing log directory growth over time as part of routine log-rotation maintenance.
Common interview questions
- What's the difference between du and df? du reports space used by specific files or directories by walking the tree; df reports space usage for an entire mounted filesystem as tracked by the kernel. They measure different things and can disagree.
- How would you find the largest directories under a path? du -h --max-depth=1 /path | sort -h, then repeat one level deeper into whichever subdirectory is largest.
- Why might du -sh report more space than you expect from summing file sizes manually? du reports actual disk block usage, which rounds files up to the filesystem's block size and includes directory entry overhead, so it's typically larger than the sum of apparent file byte sizes.
- How do you get a single total for multiple directories? du -ch dir1 dir2 dir3, where -c adds a combined grand total line at the end.
Frequently Asked Questions
How do I find the total size of a directory?
Run du -sh /path/to/dir. -s summarizes to a single total, and -h formats it in human-readable units.
How do I find the largest directories under a path?
Run du -h /path | sort -h to sort by human-readable size, or du -k /path | sort -rn for a numeric largest-first sort using raw kilobyte counts.
Why does du report a different total than df?
du sums the space used by files/directories you point it at by walking the tree; df reports space usage for the whole filesystem as tracked by the kernel. They can disagree if a deleted file is still held open by a running process (df counts it, du can't see it) or if du is only pointed at part of a filesystem.
Why does du -sh show a bigger number than the sum of the file sizes I expect?
du reports actual disk block usage, which rounds each file up to the filesystem's block size, and by default includes directory entries too. Many small files can therefore use noticeably more space on disk than their apparent byte sizes suggest.
Cheat sheet
Download a quick-reference cheat sheet for du.