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.
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
-aor -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/test01Show sizes of all sub‑directories under /haodao:
[root@haodao ~]# du -h /haodao
50M /haodao/linux/test01
100M /haodao/linux
238M /haodaoDisplay only the total size of /haodao:
[root@haodao ~]# du -sh /haodao
238M /haodaoLimit depth to 1 level:
[root@haodao ~]# du -h --max-depth=1 /haodao
100M /haodao/linux
238M /haodaoLimit depth to 2 levels:
[root@haodao ~]# du -h --max-depth=2 /haodao
50M /haodao/linux/test01
100M /haodao/linux
238M /haodaoConclusion
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
