Inodes in Linux is a data structure that stores metadata about files. The inode is short for index node.
It contains the following information about a file :
- User ID of the file
- Group ID of the file
- Device ID
- File size
- Date of creation
- Owner of the file
- File protection flag
- Link counter to determine the number of hard links
Linux stores data in the form of blocks in the memory. Inode of a file contains a list of all the blocks in which a file is stored. Whereas, the inode of a directory contains a mapping of files and directories it contains to their respective inodes.
Linux system internally identifies a file with its inode number and not its name. That is why the inode doesn’t contain the name of the file. This also helps in maintaining multiple hard links as files with different file names can point to the same inode. The symbolic name of a file is stored in the enclosing directory, not in the inode.
Viewing inode stats
The total number of inodes on a system are limited and can be accessed by using the following command:
$ df -i
The table shows the inode usage for different filesystems. IFree is the number of inodes that are free to be used. IUsed is the number of inodes in use.
Finding the inode with the ls command
You can run ls with ‘-i’ flag to get the inode number along with ls command output.
The number before each file name indicates the inode number for that file. Inodes are stored together in a table and the inode number is the index where that particular inode is stored.
An inode is allocated when the file is created. The first free inode from the table is overwritten to be assigned to the file being created.
Viewing file stats
Stats for a file or a directory can be viewed using the command :
$ stat [file_name]
Here ‘example.txt’ is a text file while ‘test’ is a directory. The stat command shows the number of memory blocks allocated for the file, inode number, number of links, and access permissions.
Inodes in Linux are used to store metadata for files and directories. Users don’t interact with inodes directly. Inodes are used by the Linux file system to identify and perform operations on the file. We hope this guide helped you understand inodes better. If you have any more questions, drop them in the comments!