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?
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? – Kusalananda Apr 26 '21 at 11:18ls -s
to our reference answer on the topic, which already explained what's going on, just didn't mention that you could get the block count withls -s
. – Gilles 'SO- stop being evil' Apr 26 '21 at 11:50