A link in Linux is a pointer that points to a directory or file. Just like a programming language, links are either pointing to a file or a directory. They are like shortcuts to a file or directory.

It’s quite easy to understand when I refer to the Windows system, which we all are conversant with. In Window System, most times, when you install a software, by default, it creates a link on the desktop, which will point to the installed files of that program. Another way to create links or shortcuts in Windows is to right-click on a file, from the drop-down menu you will see a “create shortcut” option. Once clicked, the system creates a shortcut of that file or directory in the folder where the file/directory resides.

This is similar to Linux, but in Linux, it is called links instead of shortcuts, as seen in Windows. Linux has two types of links that act differently to files or directory they try to reference. The types will be buttress further in this article.

At the end of this article, you will be rest assured to be able to create hard links and soft links to point to a file

TYPES OF LINKS

Like earlier said, there are two types of links; the soft link and the hard link. Before we go further in learning how links are created, let us try to understand the two types of links as well as the differences between the two links

Hard Link: A hard link is a new entry that references an existing file in the file system. By using the ls –l command, it displays information about a file.

The second column shows the numbers of hard links in the system. That is to say, by default, every file in the file system has one hard link created. Each hard link is assigned the same inode value, which references the original file in the system. The link and the original file has the same file content and size and removing a link will only affect the link count, but will not affect other links.

Also, note that hard links cannot be created on a directory to avoid recursive loop, and deleting the original file will not have an effect on the content of the file if a hard link is created on the particular file. That is to say, if I remove the original file of a created hard link, I can still access the content of that file through the hard link.

Let us create a file and make use of our ls –l command to view the default hard link created for that file and the hard link count

The hard link count is after the permission and before the root user as seen from the output of the command above

Soft Link: The soft link, also known as symlinks, is similar to the shortcut in Windows. It creates a link just like a shortcut that references the original file. Unlike the hard link, it can create links to both files and directories, and the file or directory can be on a different file system. If the original file of the soft link is deleted, the soft link is some worth useless.

HOW TO CREATE A HARD LINK

To create a hard link, you use the ln command, followed by the original filename and one or more hard links. The hard link can reside anywhere within the file system. After a hard link is created, it will be quite difficult to differentiate between the hard link and the original file because they both have the same node and metadata

Remember that you can only create a hard link on files and not directories. Now let us give it a try by creating a hard link of our anaconda file on Desktop

After creating this hard link, check out the file size of the hard link and that of the original file. You will see they have the same file size. It is more like a duplicate of the original file.

Please note that a hard link referencing the same file as all other hard links have the same permissions, link count, group ownership and timestamp, so if any of this information is modified, it will reflect in every other link pointing to that same file

HOW TO CREATE A SOFT LINK

To create a soft link, we use the ls –s command. Like earlier mentioned, the soft link is not a regular file but instead a special type of file that points to a regular file. Also, remember, I said a soft link could point either to a file or directory. I will be giving you a Practical example on how to point to a file and directory.

Let’s kick off on learning how to point a symlink to a file

A soft link can also point to a directory, but the soft link can as well act like a directory provided the original directory still exist; you can cd into that directory through the soft link

CONCLUSION

That’s all about a hard link and a soft link. The main thing you need to take home after reading this article is knowing the difference between a hard link and a soft link. In summary, know that a soft link is just like a shortcut in the Windows Operating system, and a hard link is more like a duplicate of an original file which retains the metadata as the original file. In soft link, once the original file is deleted, the soft link is still pointing to the file, but the target is gone. A soft link pointing to the original file which has been removed is known as a dangling soft link.

If you find any part of this article confusing or you need to contribute to this, you can drop a comment on the comment section.

Author

Comments