What are inodes in Linux?

Filed Under: UNIX/Linux
inode in Linux

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
  • Permission
  • 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.

Inode -file name
multiple files pointing to the same 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
Inode

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.

ls -i
Ls Icommand

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]
File Stat

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.

Conclusion

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!

Comments

  1. Alok Ranjan Srivastava says:

    Where are inodes stored? I think in a table. But How does this table stored?
    I also do not understand your point regarding first inode.

    1. Jayant says:

      The memory in a file system is segmented into blocks of fixed size. In fact, the stat command tells you the exact number of blocks as well. The way inodes are stored actually depends on how the file system has been implemented. For example, ext2fs/ext3fs choose to store inodes before data blocks within Block Group. Inodes are basically pre-defined fields to store the metadata. The space for inodes is allocated even before it is created. Whenever a new file is created, the metadata is written on to the first inode that is available from the table. For a deeper understanding, you should read about the implementation of Linux File system

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages