Redhat – Links between files

linux

Every file or folder created in a Linux system is associated with an iNode. The name given to the file is mapped to the inode created.

Hard Link

Linux Hardlink

The below command creates two hard links to the a new files

[root@centos9vm ~]# touch shiju_default.txt
[root@centos9vm ~]# ls -l | grep shiju
-rw-r–r–. 1 root root 0 Feb 11 18:26 shiju_default.txt

[root@centos9vm ~]# link shiju_default.txt shiju_hardlink1.txt
[root@centos9vm ~]# link shiju_default.txt shiju_hardlink2.txt

[root@centos9vm ~]# ls -l | grep shiju
-rw-r–r–. 3 root root 0 Feb 11 18:26 shiju_default.txt
-rw-r–r–. 3 root root 0 Feb 11 18:26 shiju_hardlink1.txt
-rw-r–r–. 3 root root 0 Feb 11 18:26 shiju_hardlink2.txt

Notice the value “3” in the above result of “ls” command. The 3 indicates that there are three filenames associated with the same iNode.

The below command shows the inode ID 4300558 and the files associated with it

[root@centos9vm ~]# ls -il shiju*
4300558 -rw-r–r–. 3 root root 0 Feb 11 18:26 shiju_default.txt
4300558 -rw-r–r–. 3 root root 0 Feb 11 18:26 shiju_hardlink1.txt
4300558 -rw-r–r–. 3 root root 0 Feb 11 18:26 shiju_hardlink2.txt

Deleting the default file or a hard link will not delete the inode nor the other filenames associated with the inode.

[root@centos9vm ~]# rm -f shiju_default.txt
[root@centos9vm ~]# ls -il shiju*
4300558 -rw-r–r–. 2 root root 0 Feb 11 18:26 shiju_hardlink1.txt
4300558 -rw-r–r–. 2 root root 0 Feb 11 18:26 shiju_hardlink2.txt

We cannot create hard links between files or folders in different partitions.

Soft Link

A soft-link links a filename to a default file, not to the actual inode directly.

[root@centos9vm ~]# touch shiju_default.txt
[root@centos9vm ~]# ln -s shiju_default.txt shiju_default-sLink.txt
[root@centos9vm ~]# ls -il
[root@centos9vm ~]# ls -il | grep shiju
4300559 lrwxrwxrwx. 1 root root 17 Feb 11 20:04 shiju_default-sLink.txt -> shiju_default.txt
4300558 -rw-r–r–. 1 root root 0 Feb 11 18:41 shiju_default.txt

We can create hard links between files or folders even in different partitions.