July 2, 2024
Tutorials

How to Create and Remove Symbolic Links in Linux

How to Create and Remove Symbolic Links in Linux

Symbolic Link in the Links acts like a shortcut or link to the original file. These links enable you to access files and even directories from different locations without having to physically move them. The Symbolic links do not affect the content inside the file or directory rather they create a link between the original and the target (linked file).

To create and/or remove a Symbolic link in Linux, follow this guide.

How to Create and Remove Symbolic Link in Linux?

This write-up will cover:

  • Creating a Symbolic Link in Linux systems?
  • Removal of a Symbolic Link in Linux systems?

Creating a Symbolic Link in Linux Systems?

You can create a symbolic link using:

  • Method 1: Creating a Symbolic Link with the Orignal Name
  • Method 2: Creating a Symbolic Link with a Different Name

Method 1: Create a Symbolic Link with the Orignal Name in Linux

The same or original name can be given to the symbolic link using the below procedure.

Step 1: Create Symbolic Link with Same Name

You can give the original name to create a symbolic link such as “smaple.txt”, by running the following command:

ln -s ~/Documents/sample.txt ~/Desktop/sample.txt -v
ln -s ~/Documents/sample.txt ~/Desktop/sample.txt -v

In this command, the “ln” command is used for a creating link, the “~/Documents/sample.txt” is a source where the original file is saved while the “~/Desktop/sample.txt” is a target or link name.

Step 2: Inspect the Sample.txt

You can inspect the sample.txt using the Nano editor:

nano ~/Documents/sample.txt
nano ~/Documents/sample.txt

You can see the sample text inside the sample.txt file:

nano ~/Documents/sample.txt

You will find the same text in the symbolic link, as it is just a shortcut to the original file.

Method 2: Create a Symbolic Link with a Different Name in Linux

For using different link names, follow the given below instructions.

Step 1: Use a Different Name to Create a Symbolic Link

In target or link name, you can use different names:

ln -s ~/Documents/sample.txt ~/Desktop/symlink.txt
ln -s ~/Documents/sample.txt ~/Desktop/symlink.txt

In the above command, we gave the name “symlink” in place of its original name.

Step 2: View the Symbolic Link (symlink.txt)

To view what is inside the sample.txt file, run the “cat” command to view the content on your screen:

cat ~/Desktop/sample.txt
cat ~/Desktop/sample.txt

You can see nothing has changed in the sample.txt file.

Step 3: List the Created Symbolic Links

To view if both the symbolic links have been created on your Desktop, run the command:

ls

ls

As shown above, you have created two different symbolic links from the same file.

Removal of a Symbolic Link in Linux Systems?

You can utilize the following methods to remove symbolic links:

  • Method 1: Use the “unlink” to Remove Symbolic Link
  • Method 2: Remove Symbolic Link through “rm” command

Method 1: Use the “unlink” to Remove Symbolic Link

In this command, you will see how the “unlink” command removes the symbolic link from the terminal:

unlink ~/Desktop/sample.txt
unlink ~/Desktop/sample.txt

Method 2: Remove Symbolic Link through “rm” command

The “rm” command is also used for the removal of symbolic links:

rm ~/Desktop/symlink.txt
rm ~/Desktop/symlink.txt

Verify Symbolic Links After Removal

The symbolic link files can be verified using the command:

ls ~/Desktop/
ls ~/Desktop/

If there is no file found, especially the symbolic links (i,e. sample.txt and symlink.txt), it indicates that the symbolic links have been removed from your Linux system.

Conclusion

In Linux, creating and removal of symbolic links can be done with just a simple command. To create, follow the syntax “ln -s <path/of/the/original_file> <path/of/name_symbolic_link>” while to remove the symbolic link use either “unlink” or “rm” commands in the Linux terminal.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video