Using ls to list directories and their total sizes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The ls command does not calculate total directory sizes. It shows the directory entry size (typically 4 KB on ext4), not the sum of all files inside. To list directories with their actual total sizes, you need du (disk usage). This article covers every practical approach, from the simplest one-liner to cross-platform alternatives on macOS and scripted solutions.
Why ls -lh Shows the Wrong Size
When you run ls -lh, directories appear as 4.0K (or 4096 bytes). This is the size of the directory metadata block, not the contents:
Both show 4.0K regardless of whether they contain 1 MB or 10 GB of log files. The directory entry only stores filenames and inode pointers, so its on-disk size has nothing to do with the total size of the files it contains.
The Standard Approach: du
Show Total Size of Each Directory
-s: summarize (show only the total for each argument, not every subdirectory)-h: human-readable sizes (K, M, G)*/: glob that matches only directories
Example output:
Sort by Size (Largest First)
sort -rh sorts in reverse (-r) human-readable (-h) order, so the largest directories appear at the top.
Include Hidden Directories
The */ glob skips dotfiles. To include them:
.[^.]* matches names starting with a dot but excludes . and ... The 2>/dev/null suppresses errors for non-existent matches.
Limit Depth
Show sizes for directories only one level deep (do not recurse into subdirectories):
On macOS, the flag is -d instead of --max-depth:
Show Top 10 Largest Directories
The ncdu Interactive Browser
For regular disk-space audits, ncdu (NCurses Disk Usage) provides an interactive, navigable interface:
ncdu scans the directory tree once, then lets you navigate, sort, and even delete files from within the UI. It is significantly faster for exploring large trees than repeatedly running du.
macOS-Specific Notes
macOS ships a BSD version of du that differs from the GNU version on Linux:
| Feature | GNU du (Linux) | BSD du (macOS) |
| Human-readable | -h | -h |
| Max depth | --max-depth=N | -d N |
| Apparent size | --apparent-size | Not available |
| Exclude pattern | --exclude=PATTERN | -I PATTERN |
| Block size units | 1K blocks by default | 512-byte blocks by default |
To get GNU behavior on macOS, install coreutils via Homebrew:
Combining du with Other Commands
Find Directories Over a Threshold
List directories larger than 1 GB:
Export to CSV
-b gives sizes in bytes (GNU only), which is easier to parse programmatically.
Watch for Growth Over Time
Command Reference Table
| Command | What It Does |
du -sh */ | Total size of each directory, human-readable |
du -sh */ | sort -rh | Same, sorted largest first |
du -h --max-depth=1 . | Sizes one level deep (GNU) |
du -h -d 1 . | Sizes one level deep (macOS BSD) |
du -sh .[^.]* */ | Include hidden directories |
du -sb */ | Sizes in bytes (for scripting) |
ncdu /path | Interactive disk usage browser |
df -h | Free space per mounted filesystem |
Common Pitfalls
- Using
ls -land trusting the size column for directories. That column shows the directory entry size, not the total contents. Always usedu. - Forgetting
--max-depthon large trees. Runningdu -sh /without a depth limit will traverse the entire filesystem and can take minutes. - Permission errors skewing totals. If
ducannot read a subdirectory, it silently skips it, and the reported total will be smaller than reality. Run withsudofor accurate system-wide audits. - Hard links being double-counted. By default,
ducounts each hard link separately. Usedu -l(GNU) to count all links, or the default behavior which counts each file once regardless of link count, depending on your version. - macOS vs. Linux flag differences. Scripts written for GNU
duwill fail on macOS. Use the compatibility table above or installcoreutils.
Summary
lsdoes not and cannot show total directory sizes. Useduinstead.du -sh */ | sort -rhis the most common one-liner for listing directories sorted by size.- Use
--max-depth=1(Linux) or-d 1(macOS) to limit recursion depth. - For interactive exploration, install
ncdu. - On macOS, be aware that
duflags differ from GNU/Linux. Installcoreutilsfor consistent behavior.

