Do Empty Files Consume Disk Space? Understanding Inodes in Linux
This article explores whether creating an empty file with the touch command consumes disk space on Linux, revealing that each empty file uses one inode and a small amount of directory block space, and explains inode structures, related commands, and a real‑world inode exhaustion incident.
We start by questioning if creating an empty file with touch on Linux actually consumes disk space and aim to provide a clear answer by examining the Linux file system principles.
Using touch empty_file.txt and ls -l, the file size appears as 0, but the intuition suggests some space must be used.
# touch abcdefghigklmn.txt
# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 17 17:49 empty.fileRunning df -i shows inode usage; after creating another empty file the IUsed count increases by one, confirming that a new inode is allocated.
# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
......
/dev/sdb1 2147361984 12785019 2134576965 1% /search # touch empty_file2.txt
df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
......
/dev/sdb1 2147361984 12785020 2134576964 1% /searchThe conclusion is that creating an empty file consumes one inode.
To understand what an inode stores, we look at the ext2_inode structure from the Linux source (fs/ext2/ext2.h):
struct ext2_inode {
__le16 i_mode; # file permissions
__le16 i_uid; # owner UID
__le32 i_size; # file size in bytes
__le32 i_atime; # last access time
__le32 i_ctime; # creation time
__le32 i_mtime; # modification time
__le32 i_dtime; # deletion time
__le16 i_gid; # group ID
__le16 i_links_count; # link count
__le32 i_blocks; # block count
...
__le32 i_block[EXT2_N_BLOCKS]; # block pointers
...
};Using stat we can view an inode's metadata directly:
# stat test
File: `test'
Size: 0 Blocks: 0 IO Block: 1024 regular empty file
Device: 801h/2049d Inode: 26 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-01 12:14:31.000000000 +0800
Modify: 2020-03-01 12:14:31.000000000 +0800
Change: 2020-03-01 12:14:31.000000000 +0800Each inode occupies a fixed size; on the author’s machine it is 256 bytes, as shown by dumpe2fs -h:
# dumpe2fs -h /dev/mapper/vgroot-lvroot
dumpe2fs 1.41.12 (17-May-2010)
...
Inode size: 256Interestingly, the inode itself does not store the file name. File names are kept in the directory’s data structures. The relevant ext2_dir_entry struct is:
struct ext2_dir_entry {
__le32 inode; /* Inode number */
__le16 rec_len; /* Directory entry length */
__le16 name_len; /* Name length */
char name[]; /* File name, up to EXT2_NAME_LEN */
};Thus, creating an empty file consumes one inode and also a small amount of space in the parent directory’s block to store the name and inode number.
The article ends with a real‑world incident where a machine ran out of inodes because a process kept creating empty log files, leading to service failure until the unnecessary files were removed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Refining Core Development Skills
Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
