How can a file size be zero?

It's possible because there really is no file. There's just a directory entry with a name and owner. The directory entry is logically distinct from the file. For example, the same file can have more than one name in more than one directory.

Unfortunately, the term "file" isn't always used to mean precisely the same thing. But the file size logic comes from the model where a directory entry "attaches" a file to a directory and file names and related metadata are stored in the directory.


The semantic meaning of "file size" is different from the one you are using.

There are many file sizes which are meaningful. The most common one, and the one you are seeing here, is "the number of bytes in the file." If the file is an empty text file, it may indeed contain 0 bytes. This number is important to programmers because we often need to open a file, "read all the data," and close it. We need to know how many bytes of data will be in the file so we can plan ahead.

Another meaning arises from the way most file systems store data. Most file systems store data in blocks. For example, the file system may store data in 64kB blocks, meaning it will never allocate anything which is not an even multiple of 64kB. This sounds inefficient, but it can make bookkeeping quite a lot simpler, and often simpler means faster.

A third meaning, which you are tugging at, would be the actual number of bits required on the harddrive to describe the presence of a file. This includes information that is usually stored separately from the file. For instance, in Linux, the concept of the "filename" is stored in the inode for the directory containing the file (edit: from comments, technically this is stored in the directory's data. When I wrote this, I was thinking of the small-directory case. Data smaller than 156 bytes can be stored directly in the inode). This is not a commonly used meaning, because it is terribly hard to determine without knowing tremendously deep inner workings of your file system (did you account for the space needed to store all the permissions on the file?). However, if you have a 1,000,000 byte hard drive, and want to know how big of a file fits on that hard drive, this will be a very important meaning to you!


The file name is stored somewhere else.

Your disk will have a "file system" on it, put simply a method for choosing how file names and files are represented and interpreted on the physical disk.

On most Windows disks you will be using a file system called "NTFS" (New Technology File System"), this stores filename information in the Master File Table (MFT) separate from the file contents. See the Wikipedia article on Master File Table.

The file itself will therefore be of length 0 bytes, but its entry in the MFT will still occupy some space.

Tags:

Filesystems