Linux Commands

Remove or Delete Symbolic Link Linux

In Linux, linking is a powerful concept. A symbolic link is a file that refers to a different file or directory. The link itself doesn’t contain any data of the original file. It simply points to the location of the original file, be it in the same filesystem, different filesystem, or a remote filesystem.

In this guide, check out removing or deleting symbolic link in Linux.

Prerequisites

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.

$ ls -l /usr/bin/java

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.

$ ls -l dummy_symlink.text

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.

$ rm -v <symlink>

To remove multiple symlinks, use rm as you would to remove multiple files. Use the following command structure.

$ rm -v <symlink_1> <symlink_2>

In interactive mode, rm will ask before removing each file and directory. To run rm in interactive mode, use the “-i” flag.

$ rm -i <symlink>

If the target symlink is of a directory, avoid “/” at the end of the directory name.

$ rm -v <dir_symlink>

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.

$ unlink <symlink>

If the symlink is a directory link, then don’t append “/” at the end. This tool can’t remove directories.

Broken links

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.

$ find <dir_broken_link> -xtype l

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.

$ find <dir_broken_link> -maxdepth 1 -xtype 1

Deleting broken links

Using the find command, we can delete the broken links at the same time.

$ find <dir_broken_link> -xtype l -delete

Conclusion

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.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.