What is the concept of creating a file with zero bytes in Linux?
A file is (roughly) three separate things:
- An "inode", a metadata structure that keeps track of who owns the file, permissions, and a list of blocks on disk that actually contain the data.
- One or more directory entries (the file names) that point to that inode
- The actual blocks of data themselves
When you create an empty file, you create only the inode and a directory entry pointing to that inode. Same for sparse files (dd if=/dev/null of=sparse_file bs=10M seek=1
).
When you create hardlinks to an existing file, you just create additional directory entries that point to the same inode.
I have simplified things here, but you get the idea.
touch
will create an inode, and ls -i
or stat
will show info about the inode:
$ touch test
$ ls -i test
28971114 test
$ stat test
File: ‘test’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fc01h/64513d Inode: 28971114 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/1000) Gid: ( 1000/1000)
Access: 2017-03-28 17:38:07.221131925 +0200
Modify: 2017-03-28 17:38:07.221131925 +0200
Change: 2017-03-28 17:38:07.221131925 +0200
Birth: -
Notice that test
uses 0 blocks. To store the data displayed, the inode uses some bytes. Those bytes are stored in the inode table. Look at the ext2 page for an example of an inode structure.
ls
(or well, the stat(2)
system call) tells you the size of the contents of the file. How much space the filesystem needs for bookkeeping is not part of that, and as an implementation detail, it's not something that programs in general should care or even know about. Making the implementation details visible would make the filesystem abstraction less useful.