To understand the difference between hard and symbolic links, we must first understand what they are, why they exist, and how to create them.
As you see in the above output,
$ cat source.file Welcome to OSTechNix1 displays the same data as
$ echo "Welcome to OSTechNix" >source.file7.
Let us check the inodes and permissions of
$ cat source.file Welcome to OSTechNix1 and
$ echo "Welcome to OSTechNix" >source.file7.
$ ls -lia
Sample output:
total 12 11665675 drwxrwxr-x 2 sk sk 4096 Oct 17 11:39 . 4325378 drwxr-xr-x 37 sk sk 4096 Oct 17 11:39 .. 11665731 lrwxrwxrwx 1 sk sk 11 Oct 17 11:39 softlink.file -> source.file 11665692 -rw-rw-r-- 1 sk sk 21 Oct 17 11:39 source.file
As we see in the above screenshot, the inode number [
$ cat source.file Welcome to OSTechNix6 vs
$ cat source.file Welcome to OSTechNix7] and file permissions [
$ cat source.file Welcome to OSTechNix8 vs
$ cat source.file Welcome to OSTechNix9] are different, even though the
$ cat source.file Welcome to OSTechNix1 has same contents as
$ echo "Welcome to OSTechNix" >source.file7. Hence, it is proved that soft link don't share the same inode number and permissions of original file.
Now, remove the original file [i.e
$ echo "Welcome to OSTechNix" >source.file7] and see what happens.
$ rm source.file
Check contents of the
$ cat source.file Welcome to OSTechNix1 using command:
$ cd test0
Sample output:
$ cd test1
As you see above, there is no such file or directory called
$ cat source.file Welcome to OSTechNix1 after we removed the original file [i.e
$ echo "Welcome to OSTechNix" >source.file7].
So, now we understand that soft link is just a link that points to the original file. The softlink is like a shortcut to a file. If you remove the file, the shortcut is useless.
As you already know, if you remove the soft link, the original file will still be available.
Suggested read:
- How To List Symlinks On Linux
- How To Find Broken Symlinks And Delete Them On Linux
How to create Hard Link
Create a file called
$ echo "Welcome to OSTechNix" >source.file7 with some contents as shown below.
$ echo "Welcome to OSTechNix" >source.file
Let us verify the contents of the file.
$ cat source.file Welcome to OSTechNix
The
$ echo "Welcome to OSTechNix" >source.file7 has been created now.
Now, let us create the hard link to the
$ echo "Welcome to OSTechNix" >source.file7 as shown below.
$ cd test4
Check the contents of
$ ln -s source.file softlink.file9:
$ cd test5
You see the
$ ln -s source.file softlink.file9 displays the same data as source.file.
Let us check the inode and permissions of
$ ln -s source.file softlink.file9 and
$ echo "Welcome to OSTechNix" >source.file7.
$ ls -lia
Sample output:
$ cd test7
Now, we see that both
$ ln -s source.file softlink.file9 and
$ echo "Welcome to OSTechNix" >source.file7 have the same the inodes number [
$ cat source.file Welcome to OSTechNix7] and file permissions [
$ cat source.file Welcome to OSTechNix9]. Hence, it is proved that hard link file shares the same inodes number and permissions of original file.
Heads Up: If we change the permissions on
$ echo "Welcome to OSTechNix" >source.file7, the same permission will be applied to the
$ ln -s source.file softlink.file9 as well.
Now, remove the original file [i.e
$ echo "Welcome to OSTechNix" >source.file7] and see what happens.
$ rm source.file
Check contents of
$ ln -s source.file softlink.file9 using command:
$ cd test9
Sample output:
As you see above, even if I deleted the source file, I can view contents of the
$ ln -s source.file softlink.file9. Hence, it is proved that Hard link shares the same inode number, the permissions and data of the original file.
So, what is the difference between Hard link and the normal copied file?
You might be wondering why would we create a hard link while we can easily copy/paste the original file? Creating a hard link to a file is different than copying it.
If you copy a file, it will just duplicate the content. So if you modify the content of a one file [either original or hard link], it has no effect on the other one.
However if you create a hard link to a file and change the content of either of the files, the change will be seen on both.
Let us have a look at the source.file.
$ echo "Welcome to OSTechNix" >source.file0
The source file has a single line that says - Welcome to OSTechNix.
Append a new line, for example "Welcome to Linux" in
$ echo "Welcome to OSTechNix" >source.file7 or
$ ln -s source.file softlink.file9.
$ echo "Welcome to OSTechNix" >source.file1
Now check contents of both files.
$ echo "Welcome to OSTechNix" >source.file2
$ echo "Welcome to OSTechNix" >source.file3
See? The changes we just made on
$ echo "Welcome to OSTechNix" >source.file7 are updated in both files. Meaning - both files [source and hard link] synchronizes.
Whatever changes you do in any file will be reflected on other one. If you normally copy/paste the file, you will not see any new changes in other file.
For more details, check the man pages.
$ echo "Welcome to OSTechNix" >source.file4
Conclusion
In this guide, we have discussed what is soft link and hard link in Linux, how to create softlink and hardlink with example commands and finally we explained the difference between hardlink and normal copied file.
Hope you got a basic idea of how to use symbolic or soft link and hard link in Linux.
BASHCLICommand linehard linkLinuxLinux basicsLinux commandsLinux howtoLinux tipsln commandsoft linksymbolic linkUnixUnix basics