Understanding Linux Links: Part 2
In the first part of this series, we looked at hard links and soft links and discussed some of the various ways that linking can be useful. Linking may seem straightforward, but there are some non-obvious quirks you have to be aware of. That’s what we’ll be looking at here. Consider, for example, at the way we created the link to libblah in the previous article. Notice how we linked from within the destination folder:
cd /usr/local/lib ln -s /usr/lib/libblah
That will work. But this:
cd /usr/lib ln -s libblah /usr/local/lib
That is, linking from within the original folder to the destination folder, will not work.
The reason for that is that ln will think you are linking from inside /usr/local/lib to /usr/local/lib and will create a linked file from libblah in /usr/local/lib to libblah also in /usr/local/lib. This is because all the link file gets is the name of the file (libblah) but not the path to the file. The end result is a very broken link.
cd /usr/lib ln -s /usr/lib/libblah /usr/local/lib
will work. Then again, it would work regardless of from where you executed the instruction within the filesystem. Using absolute paths, that is, spelling out the whole the path, from root (/) drilling down to to the file or directory itself, is just best practice.
Another thing to note is that, as long as both /usr/lib and /usr/local/lib are on the same partition, making a hard link like this:
cd /usr/lib ln -s libblah /usr/local/lib
will also work because hard links don't rely on pointing to a file within the filesystem to work.
Where hard links will not work is if you want to link across partitions. Say you have fileA on partition A and the partition is mounted at /path/to/partitionA/directory. If you want to link fileA to /path/to/partitionB/directory that is on partition B, this will not work:
ln /path/to/partitionA/directory/file /path/to/partitionB/directory
As we saw previously, hard links are entries in a partition table that point to data on the *same partition*. You can't have an entry in the table of one partition pointing to data on another partition. Your only choice here would be to us a soft link:
ln -s /path/to/partitionA/directory/file /path/to/partitionB/directory
Another thing that soft links can do and hard links cannot is link to whole directories:
ln -s /path/to/some/directory /path/to/some/other/directory
will create a link to /path/to/some/directory within /path/to/some/other/directory without a hitch.
Trying to do the same by hard linking will show you an error saying that you are not allowed to do that. And the reason for that is unending recursiveness: if you have directory B inside directory A, and then you link A inside B, you have situation, because then A contains B within A inside B that incorporates A that encloses B, and so on ad-infinitum.
You can have recursive using soft links, but why would you do that to yourself?
Should I use a hard or a soft link?
In general you can use soft links everywhere and for everything. In fact, there are situations in which you can only use soft links. That said, hard links are slightly more efficient: they take up less space on disk and are faster to access. On most machines you will not notice the difference, though: the difference in space and speed will be negligible given today's massive and speedy hard disks. However, if you are using Linux on an embedded system with a small storage and a low-powered processor, you may want to give hard links some consideration.
Another reason to use hard links is that a hard link is much more difficult to break. If you have a soft link and you accidentally move or delete the file it is pointing to, your soft link will be broken and point to... nothing. There is no danger of this happening with a hard link, since the hard link points directly to the data on the disk. Indeed, the space on the disk will not be flagged as free until the last hard link pointing to it is erased from the file system.
Soft links, on the other hand can do more than hard links and point to anything, be it file or directory. They can also point to items that are on different partitions. These two things alone often make them the only choice.
Now we have covered files and directories and the basic tools to manipulate them, you are ready to move onto the tools that let you explore the directory hierarchy, find data within files, and examine the contents. That's what we'll be dealing with in the next installment. See you then!
Learn more about Linux through the free "Introduction to Linux" course from The Linux Foundation and edX.