151

Why is the total in the output of ls -l printed as 64 and not 26078 which is the total of all files listed?

    $ ls -l ~/test/ls
    total 64
    -rw-r--r--   1 root root   15276 Oct  5  2004 a2ps.cfg
    -rw-r--r--   1 root root    2562 Oct  5  2004 a2ps-site.cfg
    drwxr-xr-x   4 root root    4096 Feb  2  2007 acpi
    -rw-r--r--   1 root root      48 Feb  8  2008 adjtime
    drwxr-xr-x   4 root root    4096 Feb  2  2007 alchemist
1
  • 1
    @LIU Qingyuan Secondly, this q shows multiple failures in interface. 1) -l is set to show total in cryptic way that almost no one needs. But -lh is what you need 99.9% of time. So why not make -l in MB and -lAlmostNeverNeeded in blocks? 2) no total in --help 3) no total in man 4) let us promote time waste: look in --help, go to man, remember info, read more. (if you are on windows portable bash doesn't include man nor info, so you need to google for it like I am doing now.) Finally on SO: let us punish native human questions. We reward endles readings of bad manuals.
    – CoR
    Nov 10, 2020 at 12:02

4 Answers 4

107

You can find the definition of that line in the ls documentation for your platform. For coreutils ls (the one found on a lot of Linux systems), the information can be found via info coreutils ls:

For each directory that is listed, preface the files with a line `total BLOCKS', where BLOCKS is the total disk allocation for all files in that directory.

4
  • 20
    Funnily enough, man ls on my system does not mention that line, but info coreutils ls does. How comes man ls and info coreutils ls have different information about the same command? Why isn't ls just documented once? Having two different documentations for the same command seems like set up for failure. Jan 8, 2016 at 15:08
  • 1
    info docs for coreutils stuff are often more detailed than the man pages. That's why they have a note at the end of each man page referring you to the info section for more details.
    – Mat
    Jan 8, 2016 at 15:38
  • 8
    Ah. I executed info ls and it to give the same output as info coreutils ls. What does the argument coreutils do? Jan 20, 2016 at 13:31
  • @Mat I've revised the question in hopes it can be reopened. Hopefully you can vote along... Mar 4, 2021 at 18:16
50

The Formula: What is that number?

total int = Sum of (physical_blocks_in_use) * physical_block_size/ls_block_size) for each file.

Where:

  • ls_block_size is an arbitrary environment variable (normally 512 or 1024 bytes) which is freely modifiable with the --block-size=<int> flag on ls, the POSIXLY_CORRECT=1 GNU environment variable (to get 512-byte units), or the -k flag to force 1kB units.
  • physical_block_size is the OS dependent value of an internal block interface, which may or may not be connected to the underlying hardware. This value is normally 512b or 1k, but is completely dependent on OS. It can be revealed through the %B value on stat or fstat. Note that this value is (almost always) unrelated to the number of physical blocks on a modern storage device.

Why so confusing?

This number is fairly detached from any physical or meaningful metric. Many junior programmers haven't had experience with file holes or hard/sym links. In addition, the documentation available on this specific topic is virtually non-existent.

The disjointedness and ambiguity of the term "block size" has been a result of numerous different measures being easily confused, and the relatively deep levels of abstraction revolving around disk access.

Examples of conflicting information: du (or ls -s) vs stat

Running du * in a project folder yields the following: (Note: ls -s returns the same results.)

dactyl:~/p% du *
2       check.cc
2       check.h
1       DONE
3       Makefile
3       memory.cc
5       memory.h
26      p2
4       p2.cc
2       stack.cc
14      stack.h

Total: 2+2+1+3+3+5+26+4+2+14 = 62 Blocks

Yet when one runs stat we see a different set of values. Running stat in the same directory yields:

dactyl:~/p% stat * --printf="%b\t(%B)\t%n: %s bytes\n"
3       (512)   check.cc: 221 bytes
3       (512)   check.h: 221 bytes
1       (512)   DONE: 0 bytes
5       (512)   Makefile: 980 bytes
6       (512)   memory.cc: 2069 bytes
10      (512)   memory.h: 4219 bytes
51      (512)   p2: 24884 bytes
8       (512)   p2.cc: 2586 bytes
3       (512)   stack.cc: 334 bytes
28      (512)   stack.h: 13028 bytes

Total: 3+3+1+5+6+10+51+8+3+28 = 118 Blocks

Note: You can use the command stat * --printf="%b\t(%B)\t%n: %s bytes\n" > to output (in order) the number of blocks, (in parens) the size of those blocks, the name of the file, and the size in bytes, as shown above.

There are two important things takeaways:

  • stat reports both the physical_blocks_in_use and physical_block_size as used in the formula above. Note that these are values based on OS interfaces.
  • du is providing what is generally accepted as a fairly accurate estimate of physical disk utilization.

For reference, here is the ls -l of directory above:

dactyl:~/p% ls -l
**total 59**
-rw-r--r--. 1 dhs217 grad   221 Oct 16  2013 check.cc
-rw-r--r--. 1 dhs217 grad   221 Oct 16  2013 check.h
-rw-r--r--. 1 dhs217 grad     0 Oct 16  2013 DONE
-rw-r--r--. 1 dhs217 grad   980 Oct 16  2013 Makefile
-rw-r--r--. 1 dhs217 grad  2069 Oct 16  2013 memory.cc
-rw-r--r--. 1 dhs217 grad  4219 Oct 16  2013 memory.h
-rwxr-xr-x. 1 dhs217 grad 24884 Oct 18  2013 p2
-rw-r--r--. 1 dhs217 grad  2586 Oct 16  2013 p2.cc
-rw-r--r--. 1 dhs217 grad   334 Oct 16  2013 stack.cc
-rw-r--r--. 1 dhs217 grad 13028 Oct 16  2013 stack.h
28

That is the total number of file system blocks, including indirect blocks, used by the listed files. If you run ls -s on the same files and sum the reported numbers you'll get that same number.

5
  • This simply is not true. Example: /bin/ls -s -> total 15 2 filename 3 filename2 3 filename3 3 filename4 2 filename5 2 filename6 2 filename8 2 filename9
    – Don Scott
    Oct 6, 2015 at 1:50
  • 3
    I don't know what system you are on, but for me, it is true. Example: gist.github.com/rfjakob/200f6001bf91cf801891
    – Jakob
    Oct 8, 2015 at 7:31
  • @Jakob Posted a full answer, take a look and let me know if that clears it up.
    – Don Scott
    Nov 16, 2015 at 7:58
  • This is not true in Git bash for Windows.
    – thdoan
    Mar 30, 2017 at 2:45
  • 1
    I've revised the question in hopes it can be reopened. Hopefully you can vote along... Mar 4, 2021 at 18:17
20

Just to mention - you can use -h (ls -lh) to convert this in human readable format.

1
  • Unfortunately it also converts all the listed lines to human readable format too. Mar 4, 2021 at 18:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.