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      docs

Two 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     docs

Ranking 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
FlagDescription
-sPrint only a total per directory instead of every subdirectory
-hShow sizes in human-readable units
-aInclude individual files in the output, not just directories
--max-depth=NLimit output to N levels of subdirectories
-cAppend a combined total after all listed entries
Note: Use du -h --max-depth=1 to see one level of subfolders with sizes, without the full recursive dump plain du produces.
Note: du reports actual disk blocks allocated, rounded up to the filesystem's block size, which can be slightly larger than the apparent byte count ls shows. Pass --apparent-size if you need the raw byte total instead.