Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:
How to Remove Symbolic Links
Last updated: March 18, 2024
1. Overview
Symbolic links (symlinks) provide access to files more flexibly, even if the target files are on a different file system. Regularly cleaning up unused or broken symbolic links ensures the efficiency and security of the system. Removing symlinks doesn’t affect the original files or directories they point to; it simply deletes the reference, leaving the target data intact.
In this tutorial, we’ll learn different ways to remove a symbolic link.
2. Create Symlinks
Before we dive into the details, let’s create two symlinks – one for a file and one for a directory:
$ ln -s /etc/hostname fileLink
$ ln -s /sys/kernel/ dirLink
The ln -s construct creates a symbolic link, or symlink, in the current working directory that points to a file or directory. In this case, fileLink is a symlink to the /etc/hostname file and dirLink is a symlink to the /sys/kernel/ directory.
Next, let’s verify the symlinks created with the ls command:
$ ls -l
total 0
lrwxrwxrwx 1 kent kent 12 Oct 7 23:45 dirLink -> /sys/kernel/
lrwxrwxrwx 1 kent kent 13 Oct 7 23:45 fileLink -> /etc/hostname
As we can see, we successfully created two symlinks.
Now, let’s see how to remove these two symlinks using different approaches.
3. Using the rm Command
In addition to deleting files and directories, the rm command can also be used to delete symbolic links.
3.1. File Symlink
First, let’s remove fileLink using the rm command:
$ rm fileLink
$ ls -l
total 0
lrwxrwxrwx 1 kent kent 4 Oct 7 23:48 dirLink -> /sys/kernel/
As the output above shows, we successfully removed fileLink. The syntax for deleting a symbolic link and a file are the same.
3.2. Directory Symlink
Similarly, let’s remove the dirLink that points to a directory:
$ rm dirLink/
rm: cannot remove 'dirLink/': Is a directory
The rm command refuses to remove the directory symlink and prints an error message. The message looks weird since we don’t have a directory named dirLink. What’s wrong with the command?
The problem is that we’ve added a forward slash after the symlink.
When we attempt to remove symbolic links using the rm command, we should pass only the link name to rm. No matter whether the target symbolic link points to a file or a directory, we shouldn’t include the forward slash.
Now that we understand the cause of the problem, let’s fix it and try the command again:
$ rm dirLink
Thus, the dirLink link has now been removed.
4. Using the unlink Command
The unlink command is a member of the CoreUtils package and is available on all Linux distros. The unlink command accepts only a single file or symlink at a time, so we can’t remove multiple symlinks simultaneously with it alone.
Let’s re-create the two symlinks and remove them using the unlink command.
First, we remove the fileLink link:
$ unlink fileLink
$ ls -l
total 0
lrwxrwxrwx 1 kent kent 4 Oct 7 23:50 dirLink -> /sys/kernel/
The command is pretty straightforward and works as expected.
Similar to the rm command, the unlink command doesn’t accept symlink names that end with a slash and refuses to remove them.
Therefore, we should only provide the symlink name without a trailing slash when using unlink:
$ unlink dirLink
This command removes the dirLink symlink pointing to the /sys/kernel/ directory.
Critically, if we pass the name of a regular file to the unlink command, it results in the deletion of the file, even though it’s not a symbolic link.
5. Deleting Multiple Links in One Shot
We can use the find | xargs rm combination to delete files in the find command’s result. Similarly, we can remove symbolic links using the same technique.
Now, let’s see an example:
$ tree
.
├── 2bDeleted_01.txt -> aFile.txt
├── 2bDeletedDir -> aDir
├── 2bDeletedDir_01 -> aDir
├── 2bDeletedDir_02 -> aDir
├── 2bDeleted_I_am_not_a_link.txt
├── 2bDeleted.txt -> aFile.txt
├── aDir
│ ├── 2bDeleted_etc -> /etc
│ └── keepMe_etc -> /etc
├── aFile.txt
├── keepMeDir -> aDir
└── keepMe.txt -> aFile.txt
7 directories, 5 files
The output shows that there are multiple links in the current directory and the sub-directory, aDir. Also, some link names are with the pattern 2bDeleted*, while others follow the pattern keepMe*.
Here, we aim to recursively remove all symbolic links whose names match the pattern 2bDeleted*.
We should be careful as we only want to remove symbolic links. For example, we should keep the regular file 2bDeleted_I_am_not_a_link.txt intact, even though its name begins with the target pattern, i.e., 2bDeleted.
Let’s see it in action:
$ find ./ -type l -name '2bDeleted*' | xargs -I{} rm "{}"
This command finds all the symlinks (-type l) following the naming pattern of 2bDeleted* (-name ‘2bDeleted*’) from the current directory and uses xargs to pass these symlinks to the rm command.
Alternatively, we can use unlink instead of rm in the above command and still get the same results.
Let’s verify the deletion of targeted symlinks:
$ tree
.
├── 2bDeleted_I_am_not_a_link.txt
├── aDir
│ └── keepMe_etc -> /etc
├── aFile.txt
├── keepMeDir -> aDir
└── keepMe.txt -> aFile.txt
3 directories, 3 files
The output confirms that the symlinks have been successfully removed.
Although we removed the symlinks in one go using the command above, it’s recommended to first verify the output of the find command before actually removing any symlinks or files.
6. Conclusion
In this article, we’ve learned how to remove a symbolic link using the rm and unlink commands.
Additionally, we addressed how to recursively remove multiple symbolic links following a pattern in one shot through an example. As we saw, regardless of the command, we should only pass the link names and not include a trailing forward slash.