Operations 8 min read

How to Accurately Measure Directory Sizes on Linux with du

This guide explains why the ls command shows a fixed 4 KB size for directories, introduces the du (disk usage) utility, and provides step‑by‑step examples for obtaining total, per‑directory, per‑file, and cumulative size information using various du options.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Accurately Measure Directory Sizes on Linux with du

Linux treats everything as a file, so the ls command reports a fixed 4 KB size for directories because that value represents the space used to store the directory's metadata, not the actual data contained within.

The du command (short for disk usage ) is a standard Unix tool that estimates the amount of space used by files and directories. Below are common usages illustrated with the example path /home/alvin/Documents.

Get the total size of a directory

$ du -hs /home/alvin/Documents
or
$ du -h --max-depth=0 /home/alvin/Documents/

20G    /home/alvin/Documents
-h

– display sizes in human‑readable units (K, M, G). -s – show only the summary total. --max-depth=N – limit recursion to N directory levels.

List each subdirectory’s size (including deeper levels)

$ du -h /home/alvin/Documents/ | sort -rh | head -20

20G    /home/alvin/Documents/
9.6G   /home/alvin/Documents/drive-alvin
6.3G   /home/alvin/Documents/Thanu_Photos
5.3G   /home/alvin/Documents/Thanu_Photos/Camera
... (remaining lines omitted for brevity)

Show size of every file and directory using a wildcard

$ du -hs /home/alvin/Documents/* | sort -rh | head -10

9.6G   /home/alvin/Documents/drive-alvin
6.3G   /home/alvin/Documents/Thanu_Photos
3.2G   /home/alvin/Documents/drive-mageshm
756K   /home/alvin/Documents/Bank_Details
272K   /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png
... (remaining lines omitted)

Exclude subdirectories from the size calculation

$ du -hS /home/alvin/Documents/ | sort -rh | head -20

5.3G   /home/alvin/Documents/Thanu_Photos/Camera
5.3G   /home/alvin/Documents/drive-alvin/Thanu-videos
2.3G   /home/alvin/Documents/drive-alvin/Thanu-Photos
1.5G   /home/alvin/Documents/drive-mageshm
... (remaining lines omitted)

Show sizes of first‑level subdirectories only

$ du -h --max-depth=1 /home/alvin/Documents/

3.2G   /home/alvin/Documents/drive-mageshm
4.0K   /home/alvin/Documents/alvin
756K   /home/alvin/Documents/Bank_Details
9.6G   /home/alvin/Documents/drive-alvin
6.3G   /home/alvin/Documents/Thanu_Photos
20G    /home/alvin/Documents/

Get a cumulative total with statistics

$ du -hsc /home/alvin/Documents/* | sort -rh | head -10

20G    total
9.6G   /home/alvin/Documents/drive-alvin
6.3G   /home/alvin/Documents/Thanu_Photos
3.2G   /home/alvin/Documents/drive-mageshm
756K   /home/alvin/Documents/Bank_Details
... (remaining lines omitted)

These examples demonstrate how different du options ( -h, -s, --max-depth, -S, -c) can be combined with sorting and filtering utilities to obtain precise disk‑usage information for any directory hierarchy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellcommand-linedisk usagedudirectory size
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.