Hard Link vs Soft ( Symbolic ) Link
Links in Linux can are very much similar to Pointers in programming languages. The basic difference is that,Pointers in programming languages are used to point to the other variables, likewise Link is a pointer to a file or a directory. Creating a links is very much similar to creating a shortcut to a file in order to access it.
There are two kinds of links:-
1. Hard Links
2. Soft Links or symbolic link
Hard Link :-
An hard link refers directly to the physical location of another file.
A hard link has some limitations: it cannot refer to a directory and cannot cross file system boundaries. It means that you can only create hard links to the same file system where the hard link is located.When the source of the link is moved or removed, the hard link still refer to the source.
Hard link is a bit different object when compared to a symlink. In softlink a new file and a new Inode is created, but in hard link, only an entry into directory structure is created for the file, but it points to the inode location of the original file. Which means there is no new inode creation in the hard link.
You can create a hard link with the same command “ln” like this
So, to create a hard link of gedit program on your desktop, you will use the command like this:
# ln /usr/bin/gedit ~/Desktop/gedit
Soft Link :-
A symbolic link is less limited. It can refer to a directory and can cross file system boundaries.
However, when the source of the link is moved or removed, the symbolic link is not updated.The soft link is essentially a pointer to the original file and when the original file is deleted the soft link does not point to anything and so “no such file or directory” is reported.
Symbolic link are created with the ln command. For instance, to create a symbolic link to source_file:
ln -s source_file link
Symbolic links are created with the “ln” command in linux. The syntax of the command is:
$ ln -s = This flag tells to create a symlink.
For Example, if you want to create a soft link of one fo your favorite application, like gedit, on your desktop, use the command like this:
$ ln -s /usr/bin/gedit ~/Desktop/gedit
This example shows the difference between a soft (also known as a symbolic link) and a hard link on a Linux system:
echo "hello" > a ---> create a file called "a" ln a b ---> create hard link called "b" to "a" ln -s a c ---> create soft link called "c" to "a"
Printing the contents of files a, b and c produce the same result:
cat a ---> hello cat b ---> hello cat c ---> hello
Now we remove the original “a” file:
And the difference between the two links:
cat a ---> No such file or directory cat b ---> hello cat c ---> No such file or directory