Linux

How to Create Symbolic Links in Linux – Complete Guide

Introduction

Symbolic links, symlinks, soft links, you can call them anything. The importance is not in the name but in the concept. Do you know these links are common not just in Linux but in other Unix-like operating systems, too? 

Symlinks are like the signboards that appear on a highway road mentioning a certain restaurant that will come up in a few minutes. You don’t stop your car at the signboard, rather take the queue and go straight to the restaurant if it fits your needs.  

We are here to focus on the much-talked-about symbolic links in Linux. Yes, this article will vastly focus on commands to create soft links in Linux. Also, you will discover everything from the creation to the removal of these links using the ln command.

Ln Command to Create Symbolic Links

Ln Command to Create Symbolic Links

Remember that signpost we were talking about? The Ln command helps create that signpost to make you reach your destination folder without creating a copy. The Ln command mainly creates two types of links: hard and soft. We will discuss these two links in detail in our next headline. Now, let’s see how we can perform that command for different purposes.                                     

How to create a symbolic link to the Linux directory?

Syntax: ln -s [target_directory] [link_name]
How to create a symbolic link to the Linux directory?

We will use the above syntax to create a soft link in the Linux directory. We will create the link named “Family Photos,” and the location will be the desktop. One thing to note here is that the home directory in the Unix-like operating system is denoted by the symbol “~,” and the user can select any location on the system to create the symbolic links in Linux. Check out the command below.

Command:ln -s test/target-file.txt link-file.txt
How to create a symbolic link to the Linux directory

Here, “ln” stands for the link, and “-s” stands for symbolic or soft. Here, the original name of the targeted directory was “House of Photos,” and the soft link was created by the name “Family Photos.” Also, the location of the targeted directory is “D,” and the symbolic link is “desktop.” The above example is associated with directories, which will take the user to the original directory, while a link to a file will open the original file. 

Force Overwrite Symbolic Links

The process of force overwriting a link is carried out when the user wants to replace the old one with a new one without affecting the targeted, i.e., the original directory. Take a look at the command where we are instructing the system to replace the old soft link “Family Photos” with a new one called “Love Family.” 

Syntax: ln -sf new_target existing_link
Command: ln -sf "Love Family" "Family Photos"
How to create a symbolic link to the Linux directory

Now, the existing link “Family Photos” will be converted to a new one, i.e., “Love Family”.

Deleting or Removing Links

There are two ways to remove an existing soft link in Linux. One is to use the “rm” command, and the other is to use the “unlink” command. Let’s see how to use both commands.

Syntax: rm symbolic_link
Command: rm "Family Photos"
Deleting or Removing Links

Or

Syntax: unlink symbolic_link
Command: unlink "family photos"
Deleting or Removing Links

Soft Links vs Hard Links

Soft Links vs Hard Links

Soft links in Linux are like signboards to a restaurant, and hard links are like different ways to enter that restaurant. Check out a clear point-by-point differentiation of symbolic links vs. hard links below.

  • Type: Soft links can be made for both files and directories, while hard links are only made for files.
  • Location: Symbolic links can be created anywhere on the system, but hard links can only be created on the same filesystem as the target file.
  • Targeted file effect: A soft link becomes ineffective when the targeted or original directory or file is deleted. On the other hand, a hard link remains unaffected by the deletion of the targeted file. 
  • Customization: When you edit a symlink, it makes no effect on the targeted file and directory. At the same time, any modification in the hard link is duly shown upon the targeted file.

Also Read: 50+ Linux Commands with Screenshots

Conclusion 

The whole article revolves around symbolic links in Linux. These links are extremely useful in creating a reference to reach a specific file or directory without going to the original location. Why do we use them? Soft links in Linux ensure a simpler directory structure that is easily manageable. Along with symlinks, we have hard links that are multiple directory entries pointing to the same file data on the same filesystem. Symbolic links are more flexible but do not protect against the deletion of the target, whereas hard links provide space efficiency and file data protection. 

When it comes to editing the soft links, one can use the force overwriting method with two different sets of commands, i.e., “rm” and “unlink.”This makes the link point to a different target while deleting a symbolic link removes the link without impacting the target file or directory. The choice between these actions depends on your specific requirements and whether you want to update the link to reference a new target or remove it entirely. We hope our article has addressed symbolic links on a deeper level.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *