2

I know that total tells the number of 1K blocks used by the files in directory i am working on. For some purpose i entered the command ls -lh to see this in human readable form, i saw the blocksize of all the files in the directory but to my surprise when i added the blocksize of all the files in directory they do not become equal to what total tells in the first line of output. So my doubt is, what is the reason behind this difference between these two sizes ?

LocalHost
  • 489

1 Answers1

6

ls -l with or without -h only shows the (apparent) size of the files for each of them, not their disk space usage.

To see the disk space usage, use ls -s. Then, the sum should match.

Note that that total is the sum for the files that are listed. Unless you use -A, the hidden files won't be included. If you use -a, both . and .. will (generally) also be included. Note that if the same file appears twice in the list (for instance because they're hard links to the same file), it will be counted twice.

The size is in number of 512 byte units in POSIX-compliant implementations. GNU ls (the one that comes with Ubuntu) uses 1024 byte units instead unless there's a POSIXLY_CORRECT variable in the environment ($LS_BLOCK_SIZE, $BLOCKSIZE or the --block-size option can also be used to specify arbitrary units). busybox ls always uses 1024 byte units.

Example:

$ truncate -s 15T a
$ echo > b
$ ls -s
total 4
0 a  4 b
$ ls -sl
total 4
0 -rw-rw-r-- 1 me me 16492674416640 Oct  4 21:53 a
4 -rw-rw-r-- 1 me me              1 Oct  4 21:53 b
$ POSIXLY_CORRECT=1 ls -sl
total 8
0 -rw-rw-r-- 1 me me 16492674416640 Oct  4 21:53 a
8 -rw-rw-r-- 1 me me              1 Oct  4 21:53 b
$ LS_BLOCK_SIZE=1 ls -sl
total 4096
   0 -rw-rw-r-- 1 me me 16492674416640 Oct  4 21:53 a
4096 -rw-rw-r-- 1 me me              1 Oct  4 21:53 b
$ LS_BLOCK_SIZE=1 ls -sla
total 1638400
   4096 drwxrwxr-x   2 me me           4096 Oct  4 21:53 ./
1630208 drwxr-xr-x 312 me me        1626112 Oct  4 21:54 ../
      0 -rw-rw-r--   1 me me 16492674416640 Oct  4 21:53 a
   4096 -rw-rw-r--   1 me me              1 Oct  4 21:53 b
$ ls -slah
total 1.6M
4.0K drwxrwxr-x   2 me me 4.0K Oct  4 21:53 ./
1.6M drwxr-xr-x 312 me me 1.6M Oct  4 21:55 ../
   0 -rw-rw-r--   1 me me  15T Oct  4 21:53 a
4.0K -rw-rw-r--   1 me me    1 Oct  4 21:53 b

See how a is 15TiB large but takes no space on disk (it's a sparse file), and b is one byte large but takes 4KiB on disk (uses one filesystem block).

See Why are there so many different ways to measure disk usage? for further reading.

  • yes , you are absolutely right . when i used ls -s the sizes perfectly matched but now i have another doubt. I entered the command ls -lsh and in the output i saw that even for a file(suppose some simple text file) that is of 34 bytes the disk space usage is 4k , why is it so ? . is it some sort of default value that a file will use minimum of 4k ? – LocalHost Oct 04 '19 at 20:58
  • 1
    @Noshiii, yes filesystems like ext4 which you are likely using on Ubuntu arrange file data in blocks usually 4KiB large. – Stéphane Chazelas Oct 04 '19 at 21:03