What is Symbolic Links (or Symlinks) in the Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

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

DescriptionSymbolic Links or Symlinks
Difficulty LevelLow
Root or Sudo PrivilegesMaybe
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesln
Internet RequiredNo

Types of Symbolic Links

Soft linksHard 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:

Creating a soft link in Linux

Then specify the soft link path as an argument to the file command for verification.

$ file /home/linuxtldr/Documents/file.txt

Output:

Verifying the soft link file

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:

Removing the soft link file

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:

Creating a hard link in Linux

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:

Verifying the hard link file

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:

Removing the hard link file

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.