I can't figure out why I'm getting the following results:
ls -l
tells me the size of a given file (HISTORY) is "581944":
$ ls -l HISTORY
-rw-rw-r-- 1 waldyrious waldyrious 581944 Feb 22 10:59 HISTORY
ls -s
says it is "572":
$ ls -s HISTORY
572 HISTORY
I obviously need to make the values use a comparable scale. So first I confirm that using --block-size 1
in ls -l
gives me the same result as before:
$ ls -l --block-size 1 HISTORY
-rw-rw-r-- 1 waldyrious waldyrious 581944 Feb 22 10:59 HISTORY
Then I do the same to ls -s
to get a value in the same scale:
$ ls -s --block-size 1 HISTORY
585728 HISTORY
Different results! 581944 ≠ 585728.
I tried generating comparable values the other way around, using -k
, but I get:
$ ls -lk HISTORY
-rw-rw-r-- 1 waldyrious waldyrious 569 Feb 22 10:59 HISTORY
$ ls -sk HISTORY
572 HISTORY
Again, different results, 569 ≠ 572.
I tried specifying --si to make sure both options were using the same scale, to no avail:
$ ls -lk --si HISTORY
-rw-rw-r-- 1 waldyrious waldyrious 582k Feb 22 10:59 HISTORY
$ ls -sk --si HISTORY
586k HISTORY
...again, different values: 582k ≠ 586k.
I tried searching the web but the only thing I could find that seemed relevant was this:
Some files have "holes" in them, so that the usage listed by
ls -s
(...) is less than the file size listed byls -l
."
(note that in my results the opposite happens: ls -s
returns sizes bigger than ls -l
, not smaller.)
Meanwhile, this page says that
there is no elegant way to detect Unix file holes.
So, how can I deal with this discrepancy? Which of these values can be considered correct? Could this possibly be a bug in ls
?
Short answer:
ls -l
gives the size of the file (= the amount of data it contains)ls -s --block-size 1
gives the size of the file on the file systemLet's create two files:
A sparse file of 128 bytes length (A sparse file is a file containing empty blocks, see Sparse File):
Another file with random data, also of 128 bytes size:
So, as you can see in the hex representation, both files have the same amount of data, although the content is quite different.
Now, let us look at the directory:
The first value is given by the
-s --block-size 1
option, it is the amount of space used by the file on the file system.As you can see, the sparse file takes up zero space, since the file system (
ext3
in this case) was smart enough to recognize that it only contains zeroes. Also, the file with random data takes up 1024 bytes on the disk!The value depends on how the underlying file system treats files (block size, sparse file capability, ...).
In the sixth column is the size of the file if you would read it - it is the amount of data the file contains and it's 128 bytes for both files!
ls -s
tells you the allocated size of the file, always a multiple of the allocation unit.ls -l
tells the actual size. An easy way to test: