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/destinationfile
Example:
$ ln -s /home/linuxtldr/file.txt /home/linuxtldr/Documents/file.txt
Output:
Then specify the soft link path as an argument to the file command for verification.
$ file /home/linuxtldr/Documents/file.txt
Output:
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.txt
Output:
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/destinationfile
Example:
$ ln /home/linuxtldr/file.txt /home/linuxtldr/Documents/file.txt
Output:
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.txt
Output:
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.txt
Output:
That’s all for now.
If you have any more questions regarding this topic, feel free to ask them in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.