Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks

Thursday, 2 January 2014

Understanding 'INODES' in Linux/Unix Filesystems

    The inode ( also known as Index Node) is a very basic concept related to Linux and UNIX filesystem. Each and every object/element in the filesystem is associated with an inode.

     You must be aware of the fact that, most of the countries have given a unique identity to their citizens in the form of a unique identification number ('Aadhar Card' in India and 'SSN' in the USA) to identify an individual easily and uniquely. This also leads to make all the paper work corresponding to any citizen easier to be handled.

    Just like these identification numbers for people, there is a unique identity of every member of a Linux filesystem which is known as Inode number and it uniquely exists for each and every individual file on Linux or Unix filesystems.

   In any Linux/Unix filesystem, inodes occupy only up to 1% of the available disk space, whether it is the hard disk itself or a partition present on it. The inode space is helpful to "track" the files saved in the hard disk memory. Size of each inode entry is 128 bytes. The inode entries store metadata about each object of the filesystem (file or directory) that just points to these structures and does not store any kind of data. The metadata, stored by each inode entry, may have the following information about each structure:

  • Inode number
  • Access Control List (ACL)
  • Extended attribute
  • Direct/indirect disk blocks
  • Number of blocks
  • File access, change and modification time
  • File deletion time
  • File generation number
  • File size
  • File type
  • Group
  • Number of links
  • Owner
  • Permissions
  • Status flags
   Whenever a new file is created within a directory, two things are assigned to it : a Name and an Inode number. Some people may think that any parent directory contains the entire file and all the information relevant to it which might not be always true. So we see that a directory associates a file name with its Inode number.

    When a user wants to access the contents of a file or any information related to a file then he makes use of its file name in order to do that, but internally the file name is first linked with its Inode number. Then with that Inode number, the associated Inode is retrieved. There exists a table known as Inode table, which maintains the mapping information of Inode numbers with their corresponding Inodes.

Defining an Inode

An inode is a data structure on a traditional Unix-style filesystem such as UFS or ext3. An inode stores basic information about a regular file, directory, or other filesystem object.

How to access Inode Numbers?

1) Ls -i Command
As we all know, ls is used for listing all the files within a directory. ls command when used with option -i, will display the list of files along with their inode numbers.

 ls -i


2) Df -i Command
The df command when used with option -i, will display Inode information.

df -i


3) Stat Command
Stat command is very useful in displaying the file statistics. This command also shows inode number of a file.

stat [file-name]


Another Use of Inode Numbers

    If you have a file with a name 1/1/2014 then you cannot delete such file by its name using any Unix/Linux command. The only way with which you can achieve this is to delete it using it’s inode number. Linux or UNIX doesn’t allow the users to create a file with a name like 1/1/2014, but if you are using NFS from MAC OS or Windows then it is possible to create such a file.

Find command:

You can make use of find command to remove a file as follows:

find . -inum [inode-number] -exec rm -i {} \; 

You will be prompted for confirmation, press Y to remove the file.

Let’s try to verify everything.

Create a hard to delete file name.

$ cd ~/Desktop

$ touch 1/1/2014

$ ls 

Try to delete this file using rm command.

$ rm 1/1/2014 

Find out the inode number of that file.

$ ls –i 

Now delete the file using it’s inode number and find command.
