What is an Inode?

An inode, short for “index node,” is a data structure used by Linux filesystems to store metadata about a file or directory. Each file and directory in a Linux or UNIX filesystem is associated with a unique inode number. The inode contains essential information about the file or directory, such as:

  • File type (regular file, directory, symbolic link, etc.)
  • Permissions (read, write, execute)
  • Ownership (user and group)
  • Timestamps (creation, modification, access)
  • File size
  • Number of hard links
  • Pointers to the actual data blocks on the disk

It’s important to note that the inode itself does not contain the file’s name or the actual data. Instead, it serves as a reference point for accessing the file’s metadata and locating its data on the disk.

Inode Structure and Limits

Each filesystem has a fixed number of inodes, which is determined when the filesystem is created. The number of inodes is based on the expected number of files and directories the filesystem will hold. Once all the inodes are used up, no new files or directories can be created, even if there is still free disk space available. This can happen if you have a lot of small files in the filesystem, and is quite puzzling because you tend to not think about inodes, just free space.

To view the number of inodes and their usage on a filesystem, you can use the df -i command. This will display the total, used, and available number of inodes for each mounted filesystem.

Inodes play a crucial role in the concept of hard links in Linux. A hard link is essentially an additional directory entry that points to the same inode as the original file. This means that multiple file names can be associated with the same inode, allowing the file to have multiple references without duplicating the data. So /opt/bob/is/cool and /opt/bob/is-not/cool could be the same underlying file, just showing up in two places in the same filesystem. And that’s the key — the same filesystem. If you want to cross filesystems you have to use symbolic links.

When you create a hard link to a file, the inode’s link count is incremented. The file’s data is only removed from filesystem index when the last hard link is deleted, and the link count reaches zero.

Posted in “Linux” — you might be interested in other posts in that category.