How to create and remove symbolic links in Linux
Symbolic links, often referred to as symlinks or soft links, are a powerful and versatile feature in Linux that allows users to create pointers to files and directories and in this tutorial we are going to teach you how to create and remove these symbolic links.
These soft links serve as shortcuts, providing a convenient way to access files or directories from different locations within the file system.
Understanding how to create and remove symbolic links is a fundamental skill for Linux users, and you should definitely stick around to find out how to do it.
They are offering flexibility and efficiency in managing files and organizing the file structure, so much that even Microsoft decided to take them.
That’s right, even Windows 10 and Windows 11 now have symbolic links, which is a clear testimony of their usefulness and efficiency. So let’s get started!
How to Create Symbolic Links in Linux?
To learn how to create symbolic link in Linux, we will use “ln” command.
“ln” stands for link and is a command line tool that helps you create both hard links and symbolic links. While procedure for creating hard links is more or less similar, we will focus on soft links(symbolic links) and the “-s” option.
ln -s [file] [symbolic filename]
In our example, we have created a symlink for a file “~/symlink_test/testfile.txt” in “/home/sl_user/testfile“.
With a following command you can check if symlink was created successfully. Make sure to run it in the directory of the symlink file, in our case “/home/sl_user/“.
And now you should see path to the original file.
How to create a symbolic link for a directory in Linux?
In a similar manner, we can link directories. Once a symlink is created, when you enter that new directory you will have direct access to the original one.
To show you an example, let’s make a link of Desktop on our Linux KVM VPS machine.
ln -s /home/sl_user/Desktop /home/sl_user/Videos
Beautiful! Now we can access it from within the Videos directory, and whatever changes you make there, they will be made in the original directory as well since they are linked.
How to remove symbolic link in Linux?
Removal of links is also easy, maybe even easier than the creation, since it can be done with the command you are already familiar with probably.
To remove symbolic link in Linux first navigate to place where your symbolic link is and simply use “rm” command. In our example, let’s remove Desktop symbolic link.
Be careful not to remove the actual original file or a directory.
Remember, you are supposed to remove symbolic link only. In our case above, we are removing what we have created in “/home/sl_user/Video/“, not the actual “/home/sl_user/Desktop” directory.
Symbolic links are not backups, they are essentially shortcuts that point to a certain destination.
If you were to remove original file or a directory, symbolic link would be broken too, which would mean that you have deleted files for good.
Always double-check the target of the symbolic link before removal, and be mindful that symbolic links are references, not duplicates.