Understanding Links: Hard & Soft

 

In this article, we are going to look at the Hard and Soft links in Linux File Systems.

In Linux file systems, you can work with Links. Links provides easy way to give access to the files which is somewhere in the file system.

Let’s see how it works,

 

Basically every files have a blocks which contains data. In order to access the blocks, there is a INODE in file system administration.

From the inode we will get an access to the blocks. As a human beings, it is useful to have a name which is connected with inode.

Why because human can’t deal with the inode directly, computers can. From OS perspective it is called as a Hard Link, Hard Link is the name which is directly associated with inode.

And the Hard Link is one directional only, means a hard link knows which inode it belongs to but the inode doesn’t keep the information about the hard links which are associated with it.

Difference between Hard link and Soft link is, The symbolic link is not directly pointing to the inode but it is pointing to the hard link. The symbolic link is much more flexible compare to hard link.

The hard link is directly connected with inode and it can only resides in the same file system.

The symbolic link can be created anywhere in the file system partition and even it can be created accross the servers.

The symbolic link is directly pointing to the hard link. If the hard link is deleted, the symbolic link will become invalid.

Example,

[root@server ~]# ls -il /etc/passwd
1659 -rw-r--r--. 1 root root 1592 Nov 3 2016 /etc/passwd
[root@server ~]#

i- To view the inode numbers of a file/directory.

Create Hard Link:

ln <source file> <hard link>

[root@server ~]# ln /etc/passwd myhardlink
[root@server ~]# ls -il /etc/passwd myhardlink 
1659 -rw-r--r--. 2 root root 1592 Nov 3 2016 /etc/passwd
1659 -rw-r--r--. 2 root root 1592 Nov 3 2016 myhardlink
[root@server ~]#

Here the inode number is same for the original file and the hard link.

Create Symbolic Link:

ln -s <source file> <symbolic link>

[root@server ~]# ln -s myhardlink mysymboliclink
[root@server ~]# 
[root@server ~]# ls -il /etc/passwd myhardlink mysymboliclink 
 1659 -rw-r--r--. 2 root root 1592 Nov 3 2016 /etc/passwd
 1659 -rw-r--r--. 2 root root 1592 Nov 3 2016 myhardlink
8442582 lrwxrwxrwx. 1 root root 10 Jul 9 07:54 mysymboliclink -> myhardlink
[root@server ~]#

Here mysymboliclink is pointing to myhardlink. And the inode and permissions is different for symbolic link.

Remove the hard link and see what happens,

[root@server ~]# rm -rf myhardlink 
[root@server ~]# ls -il /etc/passwd mysymboliclink 
 1659 -rw-r--r--. 1 root root 1592 Nov 3 2016 /etc/passwd
8442582 lrwxrwxrwx. 1 root root 10 Jul 9 07:54 mysymboliclink -> myhardlink
[root@server ~]# cat mysymboliclink 
cat: mysymboliclink: No such file or directory
[root@server ~]#

Here the hard link has been removed. Still you can see the symbolic link in a system but it is completely invalid.

 

Related posts

Leave a Comment