Linux中ls -l命令的total计数含义及显示原因解析
total Line in ls -l Hey there! Let's clear up your questions about that mysterious total line you see when running ls -l on Linux—it's actually more useful than it might seem at first glance.
What does total represent?
The total number is the total number of 1KB-sized disk blocks used by all the files (and directories) listed in the output. Let's break that down further:
- Linux filesystems allocate storage in fixed-size "blocks" (the exact size depends on your filesystem, like 4KB for ext4 by default). Even a tiny 1-byte file will take up at least one full block of space, since you can't allocate a fraction of a block.
- GNU
ls(the default on most Linux distros) calculatestotalby converting the actual filesystem block usage into 1KB units. So if a file uses one 4KB block, that counts as 4 in thetotalnumber.
For example, if you have two 1-byte files, each taking up a 4KB block:
$ ls -l total 8 -rw-r--r-- 1 alice alice 1 Aug 10 14:30 tiny-file-1 -rw-r--r-- 1 alice alice 1 Aug 10 14:30 tiny-file-2
Here, each file uses 4 x 1KB blocks, so 4 + 4 = 8, which is the total value shown.
Why does ls -l show this total line?
It's a quick way to see the actual disk space consumed by the listed files, not just the sum of their logical sizes.
- If you just added up the byte sizes of the files (1 + 1 = 2 bytes), that wouldn't tell you how much space they're really taking up on your drive. The
totalline fixes that by showing you the real storage footprint. - It's especially helpful when you have lots of small files—you can immediately see how much disk space they're collectively using, instead of doing the math yourself.
Note: If you use
ls -lato include hidden files, thetotalnumber will include the disk blocks used by those hidden files too. You can also adjust the block size used for calculatingtotalwith the--block-sizeflag, likels -l --block-size=4Kto show total in 4KB units.
内容的提问来源于stack exchange,提问作者Shivani Hajare

