Tech Notes

Linux inodes and Links

What is inode ?

Each inode provides a unique identifier for each file and directory on the filesystem. When a file is created, it is assigned an inode number, which is unique within the filesystem. This allows the system to manage files by their inode numbers rather than by their names, which can be changed or duplicated across different directories

inodes stores the metadata about the file inode points to. inode will store metadata regarding the file size, permissions, user/group, location on hard disk, date/time etc.

inode doesn’t store the actual file.

Check inode details:

We can get details about the inode using “stat” system call. whenever we run long listing ls -l, the command is looping and making calls to the kernel and showing the output in the shell output.

manojkumar@n36-186-058:~$ ls -li /home | grep manoj
393576 lrwxrwxrwx 1 root   root     23 Apr  1 16:11 manojkumar -> /data00/home/manojkumar

manojkumar@n36-186-058:~$ stat /home/manojkumar
  File: /home/manojkumar -> /data00/home/manojkumar
  Size: 23        	Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d	Inode: 393576      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-04-04 09:00:37.885311862 +0000
Modify: 2024-04-01 16:11:23.073950202 +0000
Change: 2024-04-01 16:11:23.073950202 +0000
 Birth: -

inodes count

since total number of inodes are per partition, we can get details of free and used inodes with below command:

manojkumar@n36-186-058:~$ df -hi
Filesystem     Inodes IUsed IFree IUse% Mounted on
udev             1.9M   362  1.9M    1% /dev
tmpfs            1.9M   591  1.9M    1% /run
/dev/sda1        7.5M  121K  7.4M    2% /
tmpfs            1.9M     2  1.9M    1% /dev/shm
tmpfs            1.9M     2  1.9M    1% /run/lock
tmpfs            1.9M    16  1.9M    1% /sys/fs/cgroup
tmpfs            1.9M     2  1.9M    1% /.syskrbonly
tmpfs            1.9M    13  1.9M    1% /run/user/0
/dev/sdb          32M  124K   32M    1% /data00
tmpfs            1.9M    13  1.9M    1% /run/user/1000
tmpfs            1.9M    11  1.9M    1% /run/user/2000
tmpfs            1.9M    11  1.9M    1% /run/user/1001

Hard links

Suppose we have a file1.txt and we create a hard link to file.txt and name it as hardlink.txt. In this case, both file.txt and hardlink.txt points to same inode. so both are pointing to same space in the partition.

Below is the example:

manojkumar@n36-186-058:~$ ln file1.txt hardlink.txt

manojkumar@n36-186-058:~$ ls -ltr
total 8
-rw-r--r-- 2 manojkumar manojkumar 13 Apr  4 10:12 hardlink.txt
-rw-r--r-- 2 manojkumar manojkumar 13 Apr  4 10:12 file1.txt

manojkumar@n36-186-058:~$ stat file1.txt
  File: file1.txt
  Size: 13        	Blocks: 8          IO Block: 4096   regular file
Device: 810h/2064d	Inode: 11796490    Links: 2
Access: (0644/-rw-r--r--)  Uid: ( 1001/manojkumar)   Gid: ( 1001/manojkumar)
Access: 2024-04-04 10:12:38.329800291 +0000
Modify: 2024-04-04 10:12:38.329800291 +0000
Change: 2024-04-04 10:12:50.937932823 +0000
 Birth: -

manojkumar@n36-186-058:~$ stat hardlink.txt
  File: hardlink.txt
  Size: 13        	Blocks: 8          IO Block: 4096   regular file
Device: 810h/2064d	Inode: 11796490    Links: 2
Access: (0644/-rw-r--r--)  Uid: ( 1001/manojkumar)   Gid: ( 1001/manojkumar)
Access: 2024-04-04 10:12:38.329800291 +0000
Modify: 2024-04-04 10:12:38.329800291 +0000
Change: 2024-04-04 10:12:50.937932823 +0000
 Birth: -
manojkumar@n36-186-058:~$

if I change file1.txt, both files will see same change:

manojkumar@n36-186-058:~$ cat hardlink.txt
regular file
manojkumar@n36-186-058:~$ cat file1.txt
regular file
manojkumar@n36-186-058:~$ vim file1.txt
manojkumar@n36-186-058:~$
manojkumar@n36-186-058:~$
manojkumar@n36-186-058:~$ cat file1.txt
regular file but hardlink too
manojkumar@n36-186-058:~$ cat hardlink.txt
regular file but hardlink too
manojkumar@n36-186-058:~$

Every file is a hard link. When a file is created, there is one hard link to that file.

Soft links/symlinks/symbolic links

soft link points to the file instead of same inode or space in the partition. soft link and actual file will have different inodes. soft links is similar as file shortcut in windows

manojkumar@n36-186-058:~$ ln -s file1.txt softlink.txt
manojkumar@n36-186-058:~$ ls -l
total 8
-rw-r--r-- 2 manojkumar manojkumar 30 Apr  4 10:16 file1.txt
-rw-r--r-- 2 manojkumar manojkumar 30 Apr  4 10:16 hardlink.txt
lrwxrwxrwx 1 manojkumar manojkumar  9 Apr  4 10:18 softlink.txt -> file1.txt

manojkumar@n36-186-058:~$ stat file1.txt
  File: file1.txt
  Size: 30        	Blocks: 8          IO Block: 4096   regular file
Device: 810h/2064d	Inode: 11796490    Links: 2
Access: (0644/-rw-r--r--)  Uid: ( 1001/manojkumar)   Gid: ( 1001/manojkumar)
Access: 2024-04-04 10:16:23.206164110 +0000
Modify: 2024-04-04 10:16:18.059110006 +0000
Change: 2024-04-04 10:16:18.063110048 +0000
 Birth: -
manojkumar@n36-186-058:~$ stat softlink.txt
  File: softlink.txt -> file1.txt
  Size: 9         	Blocks: 0          IO Block: 4096   symbolic link
Device: 810h/2064d	Inode: 11796488    Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1001/manojkumar)   Gid: ( 1001/manojkumar)
Access: 2024-04-04 10:18:20.186393761 +0000
Modify: 2024-04-04 10:18:14.896338155 +0000
Change: 2024-04-04 10:18:14.896338155 +0000
 Birth: -


Posted

in

by

Tags: