Symbolic links are very useful in Linux. They allow you to access files more flexibly. It does not matter if the files are in a different file system. But what if you no longer need the symbolic link? You'd think: "how do you remove symbolic links?"
There are different ways to do that. You can delete the link file or use commands to remove the symbolic links. Please continue reading to know more about the different methods.
Prerequisites
Before we proceed with the different methods for deleting symbolic links in Linux, let's discuss what you'd need.
Of course, the first prerequisite is a working Linux computer. After all, you would not be asking this question if you didn't have one. Also, it would be best to have some familiarity with Linux commands. If you use a Linux computer, you likely already have this covered.
Last, you need writing permission on the directory where you stored the symbolic link. Otherwise, you cannot modify it and delete the symbolic link. The message "Operation not permitted" will show on your screen.
Next, you need to use the ls - l
command. It will allow you to check if a file is a symbolic link. Furthermore, it will inform you about the file or directory the symbolic link points to. After running the command, check the first character in the output. The file is considered a symbolic link if it starts with the letter l
. Then, the symbol "- >
" shows the file the symbolic link points to.
9 Methods To Remove Symbolic Links
Now that you know the prerequisites, we will discuss how to delete symbolic links in Linux in this section.
Method 1: Remove Symbolic Links with the rm
Command
The Linux rm
command is powerful. It allows you to remove (thus the letters used in the command) objects like files, directories, and symbolic links from the system.
- To remove symbolic links using this method, type this command:
rm symlink_name
Replace symlink_name
with the symbolic link you want to remove.
If the attempt were successful, it would display no output.
It's good practice to use the one where a prompt appears before deleting the file. It allows you to check whether it is the right symbolic link you are deleting.
- The syntax is:
rm -i symlink_name
- It displays the output:
rm: remove symbolic link 'symlink_name'?
Press the Enter key or type y
to confirm.
Method 2: Remove Multiple Symbolic Links with the rm
Command
What's great about the rm
command is that it can delete multiple symbolic links at once.
- To do so, follow this syntax:
rm symlink1 symlink2
This command will remove the specified symbolic links.
Method 3: Remove Symbolic Links with the unlink
Command
Another command you can use to remove symbolic links is the Linux unlink
command. The difference between this command and the rm
command is that this one can't accept multiple arguments simultaneously. You can use it to delete only one symbolic link at a time. Furthermore, the unlink
command can't remove directories. So don't append the "/
" at the end of the symlink name.
- Type this command:
unlink symlink_name
Method 4: Delete soft links using unlink
command
Soft links are symbolic links pointing to another file or directory in another file system. You can also use the unlink
command to remove these.
- To delete symbolic links to a file or directory using this method, type:
unlink name_or_path_of_link
Method 5: Remove Soft Link to a Directory
You can delete a soft link to a directory using the rm command. Just type: rm name_or_path_to_link_to_dir
Note: It does not have -r
. That's because you only need that option when removing a directory. Here, we are only removing a link to a directory.
Method 6: Remove Hard links
Hard links are symbolic links that point to files and folders in the same system.
Here's the thing. The hard links look almost the same as the original file. So please be careful with your commands. You can check the inode number to verify if the object is the original file or a hard link.
- Once you have identified which object is a hard link, you can use this command to remove it:
rm path_or_name_of_hard_link
Method 7: Delete the Linked File
We recommend something other than this method, as it is a lengthy process. When you delete a file where a symbolic link is pointing, the link isn't automatically deleted. Instead, it becomes a broken link. Now you have to delete the broken links.
- To remove the linked file, use the command:
rm "$(readlink '/path/to/link')" /path/to/link
- To find the broken links in a directory, type:
find /path/to/directory -xtype l
Manually delete the broken links using either the rm
or unlink
command.
- Alternatively, delete them immediately after finding them with the command:
find /path/to/directory -xtype l -delete
Method 8: Remove Multiple Links in One Shot
This method is the most complicated method to delete symbolic links. But there's value in learning how to do it. Finding and deleting multiple symbolic links in one go offers convenience.
So how to do this? You use the find
, xargs
, and rm
combinations. Follow these instructions to do so:
- Type the command:
find . -type l -name '2bDeleted*' | xargs -I{} rm "{}"
The -type l
segment ensures only symbolic links show up. Meanwhile, the -name
'customname' adds a filter, showing only links with 'customname' in their names. Replace this with the text you want to use as a filter. The | xargs -I{} rm "{}
part deleted the detected links.
Method 9: Find and Remove Symbolic Links
Suppose you need to find out the name of the symbolic links. If that is the case, you can't use the rm
or unlink
command. These require you to have that knowledge. So, you must first use the find command to know their names.
- Use this command:
find . –type l
- If you want to search links only on a specific directory, use this instead:
find /path_to_directory/ -type l
Use the rm
or unlink
commands to delete the specific links you want to remove.
- But if you want to remove them all, you can use this instead to save time:
find . -type l -delete
Bonus Tip - How to Restore Accidentally Deleted Files in Linux
What if you deleted the wrong ones on your quest to delete symbolic links? Worse, you mistakenly deleted the original file. Don't panic. There's a recommendable way to recover lost data. You can use Wondershare Recoverit Linux File Recovery.
Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software
5,481,435 people have downloaded it.
Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.
Compatible with all mainstream Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.
Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.
The simple point-and-click interface allows you to recover data from Linux hard drives in just a few clicks.
Works through a remote connection. You can recover lost data even when your Linux device is crashed.
Here's a step-by-step guide on how to recover deleted files from Linux using Wondershare Recoverit.
Step 1Navigate to Linux Recovery
Download and install Wondershare Recoverit on your computer. Once done, launch it > select NAS and Linux > click Linux Recovery.
Step 2Create a Remote Connection
A new window will show on your computer screen. Fill out the form with the necessary details. Click Connect once you are done filling out the form.
Recoverit will start scanning your computer to find missing files once the connection is established.
Step 3Preview and Restore Files
The best thing about Recoverit is it gives you the liberty to stop the scanning process whenever you want. It's useful, especially when there are so many files to scan. You don't have to wait for the scanning to finish when you have already found the file you want to recover.
Preview the files to check and confirm that they are the ones that you want to recover. Finally, click Recover to restore them to a safe location.
For Windows Vista/7/8/10/11
For macOS X 10.10 or later
Summing Up
Removing symbolic links in Linux is easy as long as you know how to use the rm
and unlink
commands. If you accidentally deleted the wrong files, recovering them is just as easy. You can use Wondershare Recoverit to rescue your lost files.