Saturday, November 1, 2014

inode



inode contents

An inode is a data structure that contains metadata about a file. When the file system stores a new file on the hard disk, it stores not only the contents (data) of the file, but also extra properties like the name of the file, the creation date, its permissions, the owner of the file, and more. All this information (except the name of the file and the contents of the file) is stored in the inode of the file.

The ls -l command will display some of the inode contents, as seen in this screenshot.

root@linux~# ls -ld /home/project42/
drwxr-xr-x 4 root pro42 4.0K Mar 27 14:29 /home/project42/

inode table

The inode table contains all of the inodes and is created when you create the file
system (with mkfs). You can use the df -i command to see how many inodes are
used and free on mounted file systems.

root@linux~# df -i
Filesystem    Inodes       IUsed        IFree         IUse%      Mounted on
/dev/mapper/VolGroup00-LogVol00
4947968 115326 4832642 3% /
/dev/hda1   26104 45 26059 1% /boot
tmpfs 64417 1 64416 1% /dev/shm
/dev/sda1 262144 2207 259937 1% /home/project42
/dev/sdb1 74400 5519 68881 8% /home/project33
/dev/sdb5 0 0 0 - /home/sales
/dev/sdb6 100744 11 100733 1% /home/research

In the df -i screenshot above you can see the inode usage for several mounted file
systems. You don't see numbers for /dev/sdb5 because it is a fat file system.

inode number
Each inode has a unique number (the inode number). You can see the inode numbers with the ls -li command.

paul@linux:~/test$ touch file1
paul@linux:~/test$ touch file2
paul@linux:~/test$ touch file3
paul@linux:~/test$ ls -li
total 12
817266 -rw-rw-r-- 1 paul paul 0 Feb 5 15:38 file1
817267 -rw-rw-r-- 1 paul paul 0 Feb 5 15:38 file2
817268 -rw-rw-r-- 1 paul paul 0 Feb 5 15:38 file3
These three files were created one after the other and got three different inodes (the first column). All the information you see with this ls command resides in the inode, except for the filename (which is contained in the directory).

inode and file contents

Let's put some data in one of the files.
paul@linux:~/test$ ls -li
total 16
817266 -rw-rw-r-- 1 paul paul 0 Feb 5 15:38 file1
817270 -rw-rw-r-- 1 paul paul 92 Feb 5 15:42 file2
817268 -rw-rw-r-- 1 paul paul 0 Feb 5 15:38 file3

paul@linux:~/test$ cat file2
It is winter now and it is very cold.
We do not like the cold, we prefer hot summer nights.
paul@linux:~/test$

The data that is displayed by the cat command is not in the inode, but  somewhere else on the disk. The inode contains a pointer to that data.

No comments:

Post a Comment