Symbolic links or symlinks are one of the most important Linux features, especially for aspiring Linux sysadmins. As the name suggests, symbolic links allow users to point a file toward another file without mirroring its data, essentially providing you with multiple places to access the same file. So, in this guide, let’s look at how you can create symbolic links in Linux.
Symlinks are often referred to as soft links and it different from another type of link in Linux – hard link. The difference between hard and soft links is that a hard link to a file will be able to access the contents of the original file and will retain its contents if the original file were to be deleted. Whereas, a soft/symbolic link is like a shortcut in Windows, which points to the original file but becomes useless once the file it’s pointing to is deleted.
How to Create a Symbolic Link (Symlink) in Linux
Creating a symbolic link in Linux is simple. All you need to do is use the “ln” command along with the “-s” identifier to tell your Linux system that you are trying to make a symlink. Here, “ln” is short for Link, and this command is used to create a Symbolic Link. It’s usually used in conjunction with an identifier followed by either two file names or paths. Here’s the syntax for the ln command in Linux:
ln -identifier /path/to/the/file file
Here’s how you can create a symlink in Linux using the ln command:
1. Launch the Terminal and ensure you’re in the same directory as the file you want to link to.
2. Type the following command in the Terminal. Make sure to use the “-s” identifier followed by the name of the file and the new name. Here “1” is the actual file and 1-1 is the name of the symlink file.
ln -s 1.txt 1-1.txt
3. Similarly, you can also create a symbolic link for directories and link them to a particular folder using the ln command. In the below example, we mention the directory that we want to link first (/home/abubakar) and then mention the folder we want to link it to, which here is folder 1.
ln -s /home/abubakar folder1
How to Overwrite Symbolic Links in Linux
If you want to overwrite a Symlink to replace it with a new revision of the file you’re linking to, you can easily do so using the “-f” force parameter. Linux doesn’t allow you to overwrite existing symlinks by passing only the same file names, rather you’ll need to use the -f parameter. Here’s the syntax to overwrite symlinks:
ln -sf file_name1 file_name2
Now, let’s see how to overwrite symlinks in Linux using an example. Below, we are overwriting 1.txt with 2.txt using the ln command:
ln -sf 1.txt 2.txt ## Returns ln: failed to create symbolic link '2.txt': File exists ln -sf 1.txt 2.txt ## Works!
How to Remove Symbolic Links in Linux
Like accessing Linux files from Windows and creating symlinks, removing them is pretty easy too. Here’s how to do it.
1. For a file, you could go ahead and remove the symlink to break the link. You can do this by using Linux’s rm command in the directory which holds the symlink.
2. For folders, again, you could delete the link using the “rm -rf” command.
rm -rf "folder"
3. Here’s how to use the unlink command to unlink a folder from its symlink.
How to Find and Remove Broken Symlinks in Linux
From the “Overwrite Symlinks example” if I delete the file “1.txt,” it will create a broken symlink “2.txt.”
Now, to find this broken symlink in Linux, use the following command. If you are in the same directory as the broken symlink, you don’t need to mention the path. Just use the command syntax below:
find /home/directory_name -xtype l
Finally, execute this command to get rid of the broken Symlink:
find /home/directory_name -xtype l -delete