Understanding the Linux Inode

An inode in Linux is a file structure that holds data about a file. In Linux, everything is treated as a file. This includes directories and devices such as keyboard, mouse etc. Each of these files is associated with an inode.

The number of inodes consumed in a file system is equivalent to the number of files in that file system.

To best understand inodes, we’ll consider the questions below.

1. What is an inode number?

An inode number is also called an index number. It is a number that references a file in a file system. It is unique for each file in the file system. Inode numbers are stored in inode table.

2. What information on a file does an inode have?

An inode holds the following information about a file. It however doesn’t hold the name of the file

  • File type: eg. regular file, directory, pipe
  • Permissions to that file: read(r), write(w), execute(e)
  • Link count: Hard links relative to the inode
  • User ID: owner of the file
  • Group ID: group owner of the file
  • Size of file: size inbytes
  • Time stamp: access time, modification time and change time
  • Attributes: immutable’ for example
  • Access control list: permissions for special users/groups
  • Link to location of file

3. What is the relationship between inodes and disk space?

The number of inodes and disk space are not directly related. By this I mean, you many have a disk that is full in terms of size but has many free inodes. Similarly, you may have a disk with inodes depleted but has plenty of space still. This is because, inodes measure the number of files in the file system while disk space is a metric of how big files in the file system are. Type the commands df -h and df -i to compare your disk usage and file usage statistics.

4. What Linux commands exist that relate to inodes?

The following are some commands that can be used to get information relating to inodes.

  • Stat command The stat command shows metadata stored on an inode about a file. It will display all the information stored on the inode number of a specific file

[wm@serv0-wal Desktop]$ stat rfc1035.txt

File: ‘rfc1035.txt’

Size: 122549 Blocks: 240 IO Block: 4096 regular file

Device: fd02h/64770d Inode: 402656946 Links: 1

Access: (0664/-rw-rw-r--) Uid: ( 1000/ wm) Gid: ( 1000/ wm)

Access: 2019-02-19 18:55:39.863173618 +0300

Modify: 1987-11-24 20:31:45.000000000 +0300

Change: 2019-01-29 06:04:48.743112578 +0300

Birth: -

For more information about the stat command, see the manpage for stat command

  • df -i command

To display the number of inodes in the file system, use this command.

[wm@serv0-wal Desktop]$ df -i

Filesystem Inodes IUsed IFree IUse% Mounted on

/dev/mapper/centos-root 26214400 500675 25713725 2% /

devtmpfs 442784 461 442323 1% /dev

tmpfs 447172 58 447114 1% /dev/shm

tmpfs 447172 810 446362 1% /run

tmpfs 447172 16 447156 1% /sys/fs/cgroup

/dev/sda5 524288 372 523916 1% /boot

/dev/mapper/centos-home 24162464 182457 23980007 1% /home

tmpfs 447172 37 447135 1% /run/user/1000

  • ls -i command

This command will display the inode number of a files in the current directory. To display the inode number of a particular file, append its name after the command.

[wm@serv0-wal Desktop]$ ls -i rfc1035.txt

402656946 rfc1035.txt

  • find <dir_path> -inum <inode_no>

The find command can be used with -inum option to locate the path of a file, given its inode number.

[wm@serv0-wal Desktop]$ find /home/wm/ -inum 402656946


5.What effect does copy, move and delete operations have on inode numbers?

  • The cp command changes a files inode number When a file is copied to a new location, the a free inode number is a signed to the new copy of the file.

[wm@serv0-wal Desktop]$ ls -il rfc1035.txt

402656946 -rw-rw-r-- 1 wm wm 122549 Nov 24 1987 rfc1035.txt

[wm@serv0-wal Desktop]$ cp rfc1035.txt rfc1035_new.txt

[wm@serv0-wal Desktop]$ ls -il rfc1035_new.txt

412822792 -rw-rw-r-- 1 wm wm 122549 Feb 23 19:13 rfc1035_new.txt

  • The mv command may or may not change the inode number If the file is being moved within the same file system, the mv command does not change the inode number of the file. The number remains as it was and only the file path mapping of the inode changes.

#get current inode number

[wm@serv0-wal Desktop]$ ls -il rfc1035_new.txt

412822792 -rw-rw-r-- 1 wm wm 122549 Feb 23 19:13 rfc1035_new.txt

#move the file to a new directory

[wm@serv0-wal Desktop]$ mv rfc1035_new.txt ../Documents/

#check inode number of moved file

[wm@serv0-wal Desktop]$ ls -il ../Documents/rfc1035_new.txt

412822792 -rw-rw-r-- 1 wm wm 122549 Feb 23 19:13 ../Documents/rfc1035_new.txt

But if the file is moved to another file system, the inode number changes. A free inode is a located to the new file and the old one freed.

#get current inode numner for file as 412822792

[wm@serv0-wal Documents]$ ls -il rfc1035_new.txt

412822792 -rw-rw-r-- 1 wm wm 122549 Feb 23 19:13 rfc1035_new.txt

#move the file to /root

[wm@serv0-wal Documents]$ sudo mv rfc1035_new.txt /root

#get new inode number of same file as 100675764

[wm@serv0-wal Documents]$ sudo ls -il /root/rfc1035_new.txt

100675764 -rw-rw-r-- 1 wm wm 122549 Feb 23 19:13 /root/rfc1035_new.txt

#try find the old file using inode number – its freed

[wm@serv0-wal Documents]$ find /home/wm/Documents/ -inum 412822792

[wm@serv0-wal Documents]$

  • The rm command delete frees inodes Using rm command removes all links to a file and then frees the inode number by deleting it from the inode table.

1 Comment

  1. Here is a useful command I found in StackExchange that finds the number of inodes in a particular folder. This one finds the number of files per folder in /. The best thing about it is that it checks the folders in / without displaying inode usage recursively.

    echo “Detailed Inode usage for: $(pwd)” ; for d in `find -maxdepth 1 -type d |cut -d\/ -f2 |grep -xv . |sort`; do c=$(find $d |wc -l) ; printf “$c\t\t- $d\n” ; done ; printf “Total: \t\t$(find $(pwd) | wc -l)\n”

    Sample Output
    Detailed Inode usage for: /root
    6 – .gnupg
    2 – .pki
    2 – .ssh
    Total: 20

Leave a Reply

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