An inode is an entry in inode table, containing information ( the metadata ) about a regular file and directory. An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4.
What is inode number in Linux

Inode number also called index number, it consists following attributes.

File types ( executable, block special etc )
Permissions ( read, write etc )
UID ( Owner )
GID ( Group )
FileSize
Time stamps including last access, last modification and last inode number change.
File deletion time
Number of links ( soft/hard )
Location of ile on harddisk.
Some other metadata about file.

To check inode number of file use following command. The first field in output is an inode number of the file.

ls -il myfile.txt

1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 myfile.txt

You can also search file with an inode number using find command. For example:

find /home/rahul -inum 1150561

/home/rahul/myfile.txt

Inode Changes with Copy, Move and Delete

What happens with inode number when copy, move or delete a file on file system.

Copy file: cp allocates a free inode number and placing a new entry in inode table.

### Check inode of existing file
ls -il myfile.txt
1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 myfile.txt

### Copy file with new name
cp myfile.txt myfile_new.txt

### Check inode number of new file. Its changed
ls -il myfile_new.txt
1150562 -rw-r–r– 1 root root 0 Mar 10 01:09 myfile_new.txt

Move or Rename a file: if destination is same filesystem as the source, Has no impact on inode number, it only changes the time stamps in inode table.

### Check inode of existing file
ls -il myfile.txt
1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 myfile.txt

### Moved file to another directory
mv myfile.txt /opt/

### Check inode number of moved file. No change in inode
ls -il /opt/myfile.txt
1150561 -rw-r–r– 1 root root 0 Mar 10 01:06 /opt/myfile.txt

Delete a file: Deleting a file in Linux decrements the link count and freeing the inode number to be reused.

Using the $stat command will give you information on a files inode:

[ec2-user@imraan ~]$ stat germany.pem
File: ‘germany.pem’
Size: 1692 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 17848 Links: 1
Access: (0600/-rw——-) Uid: ( 500/ec2-user) Gid: ( 500/ec2-user)
Access: 2017-12-27 05:42:30.000000000 +0000
Modify: 2017-12-27 05:42:30.000000000 +0000
Change: 2018-01-19 09:30:55.155458748 +0000
Birth: –

Leave a Reply

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