Operations 8 min read

How to Accurately Check Directory Sizes on Linux with du and ls

This guide explains why ls -lh shows only the directory entry size, introduces the du command for real disk usage, details its most useful options, and provides step‑by‑step examples for measuring directory sizes on a Linux system.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Accurately Check Directory Sizes on Linux with du and ls

Introduction

Many users assume that ls -lh displays the total size of a directory, but it actually reports the size of the directory entry itself, typically 4 KB. To see the true disk usage of a directory, the du command is required.

Why ls -lh Is Misleading

The Linux file system stores only file names and inode numbers inside a directory. The directory file occupies one or more blocks (usually 4 KB each), so ls -lh / may show only 4 KB or 8 KB even though the directory contains much larger files.

Using du to Get Real Disk Usage

du

(disk usage) reports the amount of space actually allocated on disk, taking the filesystem block size into account. A 1‑byte file still consumes a full block (commonly 4 KB), and larger files consume multiples of the block size.

Basic Syntax

du [-abcDhHklmsSx] [-L <symlink>] [-X <file>] [--block-size] [--exclude=<path>] [--max-depth=<N>] [--help] [--version] [path]

Key Options

-a

or -all: show sizes of individual files. -b or -bytes: display sizes in bytes. -c or --total: also show a grand total. -h or --human‑readable: use K, M, G units. -H or --si: same as -h but with powers of 1000. -k or --kilobytes: display in 1024‑byte units. -l or --count-links: count hard‑linked files multiple times. -L <symlink> or --dereference: follow symbolic links. -m or --megabytes: display in megabytes. -s or --summarize: show only a total for each argument. -S or --separate‑dirs: exclude sub‑directory sizes when reporting a directory. -x or --one‑file‑system: stay on the initial filesystem. -X <file> or --exclude‑from=<file>: read exclude patterns from a file. --exclude=<path>: skip a specific path. --max-depth=<N>: limit recursion depth.

Practical Examples

Assume a test directory /haodao/linux/test01:

[root@haodao ~]# du -h /haodao/linux/test01
50M    /haodao/linux/test01

Show sizes of all sub‑directories under /haodao:

[root@haodao ~]# du -h /haodao
50M    /haodao/linux/test01
100M   /haodao/linux
238M   /haodao

Display only the total size of /haodao:

[root@haodao ~]# du -sh /haodao
238M   /haodao

Limit depth to 1 level:

[root@haodao ~]# du -h --max-depth=1 /haodao
100M   /haodao/linux
238M   /haodao

Limit depth to 2 levels:

[root@haodao ~]# du -h --max-depth=2 /haodao
50M    /haodao/linux/test01
100M   /haodao/linux
238M   /haodao

Conclusion

Understanding the difference between directory entry size and actual disk usage helps avoid confusion when managing storage. By mastering du and its most relevant options, Linux users can quickly assess how much space directories and files really occupy.

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.

linuxBashFilesystemcommand-linedudirectory 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.