Hard Link and Symbolic Link, What’s the Difference?
Symbolic Links
Let’s start with symbolic links, also known as soft links. A symbolic link is a file that points to the location of another file or directory (called the target). The soft link practically pretends to be whatever the target is. Here’s an example:
In our home directory we have a file apples.txt
in the directory textfiles
. Let’s create a symbolic link to that.
We now have a symbolic link file called apls
. We can treat it the same as the actual file, while only ever referencing the link.
As you can see, it looks identical to the apples.txt
file we created earlier. Now, not only does it look identical, but any changes made to apls
will reflect to apples.txt
and vice versa.
When Granny Smith
was appended to textfiles/apples.txt
, the change also showed in apls
.
What happens if we remove the original file, textfiles/apples.txt
?
If we remove the target file, it’s as if apls
doesn’t exist. In reality, the link file exists, but the target file it’s linking to doesn’t, thus the error.
The last speciality of symbolic links is that the link can refer to a target that is on a different partition on the drive, on a different drive, or on another drive in a network.
Hard Links
Hard links work very similarly to soft links in that the link references a target file and they change with each other. The key differences are that hard links must point to a file, not a directory and that the target file must be on the partition on the same drive as the link.
Here we have a file textfiles/alpha.txt
, let’s make a hard link to it.
So far the link types seem identical, but when we list the files, beta
looks like a normal file instead of a link (white instead of light blue).
Let’s changebeta
and make sure it reflects on textfiles/alpha.txt
Just as we expected.
Heres where the difference comes in: what happens if we delete the target file textfiles/alpha.txt
?
The link still works! But how?
This is made possible by the fact that hard links point to the data rather than the file, so when the file is removed, the data can remain.
Conclusion
It seems like hard links would be your best bet, unless you need to refer to a directory or refer to something on another drive.