Symbolic links, sometimes also referred to as “symlinks”, are used to create file pointers pointing towards files and directories identical to Windows shortcuts in a Linux system.
In this article, you will learn different types of symbolic links, how to create them, and then how to remove them.
Tutorial Details
| Description | Symbolic Links or Symlinks |
| Difficulty Level | Low |
| Root or Sudo Privileges | Maybe |
| OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
| Prerequisites | ln |
| Internet Required | No |
Types of Symbolic Links
| Soft links | Hard links |
|---|---|
| Stores the address or location of a file as a string. | Duplicate the original file. |
| Deleting the soft link does not affect the original file. | Deleting the hard link also does not affect the original file. |
| Deleting the original file will break the soft link. | Deleting the original file does not break the hard link; instead, it remains as the original file. |
| Changing the location of the original file will break the soft link. | Changing the location of the original file does not affect the hard link. |
| Do not occupy extra space on the storage device. | Make room on the storage device for a duplicate of the original file. |
Creating Soft links for Files and Directories
The ln command with the “-s” flag is used to create a soft link in Linux that can be followed by providing the full path of the original file and destination where the soft link will be created.
Syntax:
$ ln -s /path/of/the/orginalfile /path/of/the/destinationfileExample:
$ ln -s /home/linuxtldr/file.txt /home/linuxtldr/Documents/file.txtOutput:

Then specify the soft link path as an argument to the file command for verification.
$ file /home/linuxtldr/Documents/file.txtOutput:

In the future, if you modify anything in the soft link, it will directly reflect the original file.
If you want to remove the soft link, you can either delete the soft link file or provide the full path of the soft link file as an argument to the unlink command.
$ unlink /home/linuxtldr/Documents/file.txtOutput:

Creating Hard links for Files and Directories
If you are able to create a soft link, then you can easily create a hard link by just ignoring the “-s” flag from the “ln” command.
Syntax:
$ ln /path/of/the/orginalfile /path/of/the/destinationfileExample:
$ ln /home/linuxtldr/file.txt /home/linuxtldr/Documents/file.txtOutput:

Now, you can use the file command to check that the hard link is a duplicate copy of the original file and has its own existence in your system, unlike soft links.
$ file /home/linuxtldr/Documents/file.txtOutput:

In the future, if you modify anything in the hard link, it will directly reflect on the original file, and deleting the original file will also not affect the hard link.
If you want to remove the hard link, you can either delete the hard link file or provide the full path of the hard link file as an argument to the unlink command.
$ unlink /home/linuxtldr/Documents/file.txtOutput:

That’s all for now.
If you have any more questions regarding this topic, feel free to ask them in the comment section.





There are several major errors in this article.
First and foremost, a hard link is not a type of symbolic link. “Symbolic link” and “soft link” are synonyms. “Hard link” is by definition (in the POSIX standard) just another term for directory entry. See https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_104
Secondly, the article says creating a hard link to a file duplicates the original file. This is false. It just makes the same file appear under more than one name (which can be in a different directory). As per the above POSIX definition, “Several directory entries can associate names with the same file.”
This means the table entry about occupying extra storage space is backwards. Disregarding the possible need to extend the directory that the new name is being added to (which is the same in both cases), creating a hard link does not use more storage space, whereas creating a soft link does use more storage space (for the string).
The statement that a soft link can be created using ln -s by “providing the full path of the original file and destination where the soft link will be created” is true, but it’s misleading because it implies a full (i.e. absolute) pathname is required. It is also possible to create a soft link using a relative pathname, although care must be taken because it is relative to the directory containing the destination file, not to the shell’s current directory when executing ln (if they are different; it helps to avoid confusion if you make sure they are the same).
Finally, there are oddities regarding references to the “unlink” utility. This is being suggested as an alternative to “deleting” a file, as if it does something different than rm. But in reality, rm and unlink will both end up doing exactly the same thing when used as described here, which is to remove a directory entry. The unlink utility is an obscure piece of UNIX history which it would be best not to mention at all in the article.