Detailed Understanding Of Linux Inodes With Example

February 1, 2013 | By
| Reply More

"Everything is a file": Linux and other Unix-like Operating systems maintain a consistency by treating everything as a file (even the hardware devices). The keyboard, mouse, printers, monitor, hard disk, processes, even the directories are treated as files in Linux. The regular files contain data such as text (text files), music, videos (multimedia files) etc. Set aside the regular data, there are some other data about these files, such as their size, ownership, permissions, time stamp etc. This meta-data about a file is managed with a data structure known as an inode (index node).

Note :

Inode limits is per filesystem and decided at filesystem creation time. Maximum directory size is filesystem dependent and thus the exact limit differs. For better performance make your directories smaller by sorting files into subdirectories rather having one large directory.

Inode table and Inodes

Linux extended filesystems such as ext2 or ext3 maintain an array of these inodes: the inode table. This table contains list of all files in that filesystem. The individual inodes in inode table have a unique number (unique to that filesystem), the inode number. Diving deep into the inode, an inode stores:

• File type (regular file, directory, pipe etc.)
• Permissions to that file (read, write, execute)
• Link count (will discuss later)
• User ID (owner of file)
• Group ID (group owner)
• Size of file (or major/minor number in case of some special files)
• Time stamp (access time, modification time and (inode) change time)
• Attributes ('immutable' for example)
• Access control list (permissions for special users/groups)
• Link to location of file
• Other metadata about the file

(Note that inode does not store name of the file)

Below Commands helps to find more information about inode

Stat

$ stat myfile
File: `myfile'
Size: 36 Blocks: 1 IO Block: 4096 regular file
Device: 802h/2050d Inode: 2501 Links: 1
Access: (0600/-rw-------) Uid: ( 1000/ raghu) Gid: ( 1000/ raghu)
Access: 2012-07-21 00:09:19.362944000 +0530
Modify: 2012-06-25 05:46:32.000000000 +0530
Change: 2012-06-26 17:10:45.945142000 +0530

ls -li

The -i option with “ls” displays the inode number

$ ls -li
total 1
2501 -rw------- 1 raghu raghu 36 Jun 25 2012 myfile
2497 -rw------- 1 raghu raghu 22 Jun 25 2012 samplefile.txt
2503 -rw------- 1 raghu raghu 0 Jun 25 2012 testfile
2508 drwx------ 1 raghu raghu 0 Jun 25 2012 working

df command

Displays the number of inodes used and free on the file system.

$ df -i

Displays all information of inode.

tune2fs

Displays all information related to inode.

$ tune2fs -l /dev/DEVICE | grep -i inode

Directories and inodes

As stated above, the directories in Linux are also treated as files. Directory is special file that 'maps' a file name to its inode number (this mapping is called directory entry or dentry). So when we say that a directory 'contains' files and other directories, we mean that this directory is mapping those files and directories (directories are (special) files, so they also need mapping to their inode numbers) to their inode numbers. This is the reason why a directory cannot hold two files with same name, because it cannot map one name with two different inode numbers.

$ ls -ld working/
drwx------ 1 raghu raghu 0 Jun 25 2012 working/

(The file type of directory is 'd': the first letter of the output. For regular file, its '-')
As a file is mapped to its inode by its parent directory, then how is topmost directory, (i.e. / directory) mapped to its inode? The inode number of "/" directory is fixed, and is always 2.

Links in Linux

In the output of ls -l, the column following the permissions and before owner is the link count. Link count is the number of hard links to a file. To understand hard links, we begin with links:

A link is a pointer to another file. In Linux world, two types of links exist:

• Symbolic links (or soft links)

$ ls -l myfile.softlink
lrwxrwxrwx 1 raghu raghu 20 Jan 11 12:01 myfile.softlink -> myfile

Here, “myfile.softlink” is a symbolic link to “myfile”. This symbolic link is another special file (Note letter 'l' in the beginning of above output) that points to <target> file ('myfile' here). The contents of a symbolic link are the name of target file only. If observed carefully, the size of symbolic link is equal to the number of characters of its target (one byte for one character). There are 6 characters in “myfile”. To create a soft link, run "ln –s <target> <link_name>":

$ ln -s samplefile.txt linksoft
$ ls -li samplefile.txt linksoft
3739 lrwxrwxrwx 1 raghu raghu 36 Jan 11 12:02 linksoft -> samplefile.txt
2497 -rw------- 1 raghu raghu 22 Jun 25 2012 samplefile.txt

Most of the operations (such as copy) performed on soft links will affect the actual linked file (with the exception of rm or mv commands, which remove (or move) soft link itself).

• Hard links

To get an idea of what a hard link is, it is important to understand that the identity of a file is its inode number, not its name. A hard link is a name that references an inode. It means that if 'file1.txt' has a hard link named 'file2.txt', then both of these files refer to same inode. if we create a hard link to file 'newfile' with "ln newfile newfile.hrd", and check the inode number of these two files, both of these are same:

$ ls -li newfile
3744 -rw------- 1 raghu raghu 0 Jan 11 12:04 newfile
$ ln newfile newfile.hrd
$ ls -li newfile newfile.hrd
3744 -rw------- 2 raghu raghu 0 Jan 11 12:04 newfile
3744 -rw------- 2 raghu raghu 0 Jan 11 12:04 newfile.hrd

As you must have noted, unlike soft links, hard links are no special files. Now, link count is the number a file has been hard linked. So a link count increases after creating a hard link as you can see in the above figure. These hard links have two limitations:

1. The directories cannot be hard linked. Linux does not permit this to maintain the acyclic tree structure of directories.

2. A hard link cannot be created across filesystems. Both the files must be on the same filesystems, because different filesystems have different independent inode tables (two files on different filesystems, but with same inode number will be different).

Linux operations with files and its relation with inodes

Here are some file operations in which inodes play their vital role:

• cp: copy files

When we copy a file, a new file with a new inode is created.

$ cp myfile ..
$ ls -li myfile ../myfile
2501 -rw------- 1 raghu raghu 36 Jun 25 2012 myfile
3746 -rw------- 1 raghu raghu 36 Jan 11 12:05 ../myfile

• mv: move files

When moving across filesystems, 'mv' command proceeds as 'cp' command above, with the exception that the original file is removed from its location. But when moving within a filesystem, the inode does not change, only the directory mapping of the inode is changed, the actual data on the hard disk (contents of file) does not move.

$ ls -li samplefile.txt
2497 -rw------- 1 raghu raghu 22 Jun 25 2012 samplefile.txt

$ mv samplefile.txt ..
$ ls -li ../samplefile.txt
2497 -rw------- 1 raghu raghu 22 Jun 25 2012 ../samplefile.txt

• rm: remove files

When rm command is issued, first it checks the link count of the file. If the link count is greater than 1, then it removes that directory entry and decreases the link count. Still, data is present, nor is the inode affected. And when link count is 1, the inode is deleted from the inode table, inode number becomes free, and the data blocks that this file was occupying are added to the free data block list.

$ ls -li myfile myfile.hardlink
2501 -rw------- 2 raghu raghu 36 Jun 25 2012 myfile
2501 -rw------- 2 raghu raghu 36 Jun 25 2012 myfile.hardlink

$ rm myfile.hardlink

$ ls -li myfile
2501 -rw------- 1 raghu raghu 36 Jun 25 2012 myfile

Filed Under : LINUX COMMANDS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.