0

As a very basic linux user, I've always considered the file size as shown in ls -lh to be the one and only file size on disk.

For example here, file.xlsx is 11kb

-rw-r--r--@  1 michael  staff    11K May 29  2020 file.xlsx

However, I noticed with the -s flag, the filesize displayed is now 24.

   24 -rw-r--r--@  1 michael  staff    11K May 29  2020 file.xlsx

I eventually found this answer which explained that -s shows the file size on the file system, along with the following example:

# truncate -s 128 f_zeroes.img
# hexdump -vC f_zeroes.img 
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080

# truncate -s 128 f_zeroes.img
# hexdump -vC f_zeroes.img 
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080

and when I run ls -lsh I get the following:

# ls -lsh
total 4.0K
4.0K -rw-rw-r-- 1 ubuntu ubuntu 128 Apr 26 18:44 f_random.img
   0 -rw-rw-r-- 1 ubuntu ubuntu 128 Apr 26 18:43 f_zeroes.img

The explanation is that f_zeroes.img takes up 0 on the filesystem because it's still all 0s, whereas f_random.img takes up more space due to actual characters.

My question is thus: I'm sure both values have it's usage, and so which value matters when? If I have all these files taking up 0 on the file system, does that mean I am theoretically using less space than what the -l flag is telling me?

terdon
  • 234,489
  • 66
  • 447
  • 667
Michael Du
  • 109
  • 2
  • 1
    The `24` that is shown is the number of file system blocks used by the file. You may want to read [Why are there so many different ways to measure disk usage?](https://unix.stackexchange.com/q/120311) – Kusalananda Apr 26 '21 at 11:18
  • 1
    I've added an explicit mention of `ls -s` to our [reference answer on the topic](https://unix.stackexchange.com/questions/120311/why-are-there-so-many-different-ways-to-measure-disk-usage), which already explained what's going on, just didn't mention that you could get the block count with `ls -s`. – Gilles 'SO- stop being evil' Apr 26 '21 at 11:50

0 Answers0