What is inode in Linux ?

What is inode in Linux ?

An inode is an entry point of any table. It acts as the index to the a particular cell of table. The os relates all files and directories via inodes. Inodes are basically assigned by the inside content of the file or directory on the basis of hashing. The hashed value is what we call inode number.
A file’s inode number can be found via the command line

     ls –i
  stat <filename>
  stat <directory name>

This command will print the inode number of the associated file. This will also work for the directory. So you see inode number as an address of the data which you want to find.

Remember in language C we use arrays and each cell had an index number.
In the similar way UNIX or Linux arrange all sorts of file in a table (or you may say array) and gives them an inode number (or you may say an index number)

Now we create a file named "hfile_new" then running the command above we get

Running command  stat hfile_new we get :

 Have a look on the INODE : 8725724278199526. This is the inode number of file named "hfile_new". Also note that there are two links

Q: Why this has 2 links ?

Running command ls -ia we get : 

The numbers on the left of some names are their inode numbers. 
Now did you notice single "dot" and double "dots" in green color.

dot 5629499534382291
dot dot 15481123719192815

Time for some magic !!!!!
You must be seeing this address :

The directory "linkf" is inside directory "new". So if I give a command cd .. then bash will jump to new directory.
Now I will run the command stat linkf and I get this :

Note down the following Inode: 5629499534382291
Does this match with the number of dot

Indeed !! This inode number is of "linkf"
So from 2 links one refers to the parent inode number so the OS remembers atleast from where has it came. 

Pretty Cool huh.

Now doing again cd .. and jumping back to directory abcdef
we run the command stat new we get this:

Note the inode number. This will certainly match with the inode number of dot dot
Thus we come to a conclusion that dot represents inode of self (here its is linkf) and dot dot represents inode of its parent directory (here it is new).

Pat your back. You learnt something awesome. :) 

How to find a file using inode?

Note the path here its : ~/abcdef/new/linkf
So to find the path using inoe the command is 
find <path> -inum <inode no.>

This is what one will get 

The search results "hardlink" and "hfile_new" with inode no. as given below :

How to delete a file using inode ?

By using the command 
find <path> -inum <inode no.> -exec -rm -f {} \;

How much inodes used ? 

By using the command 
df -i

Want some thing more about inodes ? Feel free to contact.
So Long :) 

No comments:

Post a Comment