0

I am on Ubuntu 20.04.5 and my file system is ext4 . I create an empty file and check its allocated size:

➜  touch test_file
➜  ls -s
total 0
0 test_file

I then echo one character into the file and check its allocated size again:

➜  echo "." > test_file
➜  ls -s
total 4
4 test_file

I understand that 4kb is the blocksize of my file system. But what happened to the file when I wrote to it? Why was there no space allocated before?

1 Answers1

2

As this is ext4, without any data in it, the only information that exists is the filename test_file, the permissions, and the owner and group which are stored in the data for the directory. The inode contains the information on the actual size of the file which is zero after creating it and 4K after appending . to it.

Until data is written to the actual file, it doesn't use up any of the blocks on the disk to store the data. That is why the empty file is showing as 0 but 4K after you wrote to it.

Think of it as the address of your house. The address is just a way for the state, city, locale, post office, and others to know its location. It doesn't provide any information on what's inside of your house such as the square footage, rooms, appliances, etc or what's going on inside. That's effectively what the filename is.

Nasir Riley
  • 11,422