A soft link is a short pointer file that links a filename to a pathname. It's nothing more than a shortcut to the original file, much like the Windows OS's shortcut option. The soft link serves as a pointer to another file without the file's actual contents. It allows the users to delete or the soft links without affecting the original file's contents.
How to create Soft Link
Let us create one file & Write Some content in that file & view the file
Now, create the soft link file from source file
Let us compare the data of both sourcefile & softlinkfile
As you see in the above output, softlinkfile displays the same data as sourcefile
Let us check the inodes and permissions of softlinkfile & sourcefile
As we see in the above screenshot, the inode number (9254978 vs 9254979) and file permissions (lrwxrwxrwx vs -rw-r--r--) are different, even though the softlinkfile has same contents as sourcefile. 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 sourcefile) and see what happens.
As you see above, there is no such file or directory called softlinkfile after we removed the original file (i.e sourcefile).
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 see above, we deleted softlinkfile its not effected to the source file.
What is the hard link?
A hard link is just another file that points to the same underlying inode as the original file. And so, it references to the same physical file location.
How to create Soft Link
Let us create one file & Write Some content in that file & view the file
Now, create the hard link file from source file
Let us compare the data of both sourcefile & hardlinkfile
As you see in the above output, hardlinkfile displays the same data as sourcefile
Let us check the inodes and permissions of hardlinkfile & sourcefile
Now, we see that both hardlinkfile and sourcefile have the same the inodes number (11665692) and file permissions (-rw-r--r--). Hence, it is proved that hard link file shares the same inodes number and permissions of original file.
Now, remove the original file (i.e sourcefile) and see what happens.
As you see above, even if I deleted the source file, I can view contents of the hardlink.file. Hence, it is proved that Hard link shares the same inode number, the permissions and data of the original file.
Liked This Article?
Conclusion
In this article, we have discussed what is soft link and hard link in Linux, how to create softlink and hardlink with example commands