In this guide, check out removing or deleting symbolic link in Linux.
A symbolic link is a pointer file. When accessed, it redirects to the location of the original file or directory. So, the rules of file permission apply similarly. To perform any operation on the symlink, the user account has to have write permission to the parent directory. Otherwise, trying to remove the symbolic link will result in an error.
If a symlink is in use, then removing it may cause unwanted problems. So, before removing a symlink, ensure that it doesn’t impact the workflow of any other program.
Detecting symbolic link
When a symbolic link is removed, the original file isn’t affected. If your goal is to remove the symlink, it’s always a good idea to double-check whether the target is actually a symbolic link.
We can determine a symbolic link from its file information.
Have a look at the file permission. The first character is “l”, indicating that it’s a symbolic link. Later in the output, it also indicates the original location of the file.
Removing a symbolic link
A symbolic link itself is a file. We can remove the link by simply deleting the symlink file. Alternatively, there are dedicated tools to detect and remove symlink.
For demonstration, I’ve made a dummy symbolic link. Learn more about how to create a symbolic link in Linux.
Removing a symlink using rm
The rm command is the dedicated tool for deleting files and directories from the system. Because the symlink itself is a file, we can use the rm command to remove it.
The following rm command will remove the symlink.
To remove multiple symlinks, use rm as you would to remove multiple files. Use the following command structure.
In interactive mode, rm will ask before removing each file and directory. To run rm in interactive mode, use the “-i” flag.
If the target symlink is of a directory, avoid “/” at the end of the directory name.
If you include “/”, then rm will assume it’s a directory. If rm is run to delete a directory, then it’ll delete contents of the original directory as well. When deleting symlink, that’s probably not the expected outcome.
Removing a Symlink Using Unlink
Instead of using rm, unlink is also an effective tool to remove symbolic links. It’s a dedicated tool to remove links (both symbolic and hard links). Unlike rm, however, unlink supports the removal of one file at a time.
To remove a symbolic link, use the following unlink command.
If the symlink is a directory link, then don’t append “/” at the end. This tool can’t remove directories.
A symbolic link is a pointer to the original file. It remembers the location of the original file at the moment of creation. If the original file is moved to a different location, the symbolic link is rendered broken.
If you’re working with symbolic links regularly, you may often come across various broken symbolic links. Broken symbolic links can be confusing and result in unwanted situations.
Finding broken links
To find a broken link under a certain directory, run the following command.
In the output, the find command will list all the broken symbolic links found.
By default, find will traverse all the sub-directories to search for broken symlinks. However, we can exclude symlinks that are contained within sub-directories.
Deleting broken links
Using the find command, we can delete the broken links at the same time.
Symbolic links are basically pointer files, so you can remove them like a normal file. Alternatively, you can also use the dedicated tools to remove symlinks from the system. Make sure that you’re not removing any symbolic link that’s a part of an important function, for example, symlinks under the directory “/usr/bin”.
Having to work with hard links? Unlike symbolic links, hard links, as the name suggests, is more persistent. Although similar in concept, hard link and symlinks are different. Check out this guide on hard link on Linux.