Bash Directory Usage
Learn how to total up the size of a directory tree using du, and how -sh gives you one clean human-readable number per folder.
Measuring Folder Size with du
The du command, short for disk usage, walks a directory tree and adds up the size of every file it finds, descending into subdirectories automatically. Run without arguments it prints a line for every subdirectory visited, which quickly turns into a wall of numbers on a large tree.
Example
$ du docs
4 docs/images
12 docs/drafts
20 docsTwo flags fix that noise. -s summarizes, collapsing the whole tree into a single total per argument instead of one line per folder. -h converts that total from kilobytes into human-readable units. Combined as -sh, this is the command most people reach for first.
Example
$ du -sh docs
20K docsRanking the Biggest Folders
A common follow-up question is which folder is eating the most space. Expand the glob so du reports on every entry in the current directory, then pipe through sort -rh to rank by size, largest first, using sort's human-numeric comparison.
Example
$ du -sh */ | sort -rh | head -5
2.1G node_modules/
340M dist/
88M .git/
12M src/
3.4M public/- -s summarizes, printing one total per argument
- -h shows sizes in human-readable K, M, G, T units
- -a includes individual files, not just directories
- --max-depth=N limits the recursion depth shown
- -c appends a grand total line after the listing