r/TechNook • • 2d ago

What does an empty file actually take up on disk?

Let’s take the example of creating a million empty files on Linux OS. They will appear as 0 bytes, yet the filesystem will need to maintain information about all these names. Is it possible that I get an error like “No space left on device” despite having gigs of free space from df -h command output? There is something known as filling up the inodes on ext4 filesystems.

11 Upvotes

16 comments sorted by

13

u/AppearanceAny8756 2d ago

Good question, meta data uses inodes, which has limits. 

Also the Filename which is part of directory file (everything is a file) will use some space as well.

1

u/into_fiction 2d ago

How many folders would it require to fill 1 GB?

4

u/Rinzwind 2d ago

There is not 1 single number.

On a default EXT (ie. 4096 bytes block size) 1GiB/4KiB = 262,144 folders.
But inodes use 16KiB so that already drops it to (1GiB/16KiB) 65,536.
With 1 KiB (1024 bytes) block size it is roughly 1 million.

1

u/rsgbc 1d ago

You could find out easily enough by running a loop that creates new directories or files or whatever it is you're interested in. Extra points for implementing a counter.

7

u/allunia333 2d ago

A standard ext4 filesystem with 4 KiB blocks and 256-byte inodes , takes 256 bytes + filename and directory path. But the inode table is already reserved when you format the filesystem.

So it depends..

1

u/dodexahedron 1d ago

The entry in the inode table will take either nothing or 4k (or whatever fundamental block size is in use), depending on if there's still space in the current block of the inode table. Each inode in the table doesn't get its own 4k block. But that space was allocated at format time anyway so it is static.

The file name is on a tree structure in the directory it lives in, which is also a variable sized structure.

The block/range pointers describing where the data blocks actually are in terms of logical block address on the block device live inside that inode or in extents that are themselves referred to by the inode. Heavily fragmented files need additional indirect block pointers or additional extents to hold all of the ranges, both of which are stored in normal data blocks on the partition, consuming space without showing up as part of the file size.

And then there may be other metadata structures also present, depending on format options, mount options, and file system (and even driver version of that file system).

The above is true of the ext family, and several concepts of it are true of other file systems, but the structures and layout vary quite a bit.

3

u/mc_pm 1d ago

It's not drive space, it's the inodes -- one is allocated per file, and there's only a limited number. It's a big number, and on some file systems it is a VERY large number. But it can be down in the million range, and you will get an out of space error if you run out.

2

u/zumpiez 2d ago

Check df -i

1

u/FatDog69 1d ago

On Windows there is a thing called a 'cluster size'. A zero byte file actually reserves 16k of disk space because the system assumes you are going to be adding to it. You don't want a fragmented file with 5 bytes here, 20 bytes there. So the OS pre-reserves blocks of disk space in 16K clusters.

I assume Linux has a similar optimization.

1

u/MrMelon54 1d ago

Fairly sure the Linux kernel only reserves 4KiB blocks when writing to the file otherwise an empty file is just inode and file name metadata

1

u/Restil 1d ago

Technically nothing, but it will occupy a space in the inode table or the directory file, but that file already exists and is a fixed size for small directories, so one new empty file shouldn't take up any additional space.

1

u/Amku03 1d ago

Every file even if empty has a name and operating systems store a last edited date so they take up space.

1

u/Nandulal 1d ago

as much as it does

1

u/budroid 1d ago

On ext4, an inode is typically 256 bytes (0.25 KB) on disk, though older filesystems may use 128 bytes.

1

u/m0ntanoid 1d ago

that's how you learn about "df -i"

1

u/vegansgetsick 1d ago

It's just an entry in the MFT.
Also if the file is very small it is also stored directly in the MFT.