Symbolic links (symlinks) are very important when managing files under Linux since they allow users to make shortcuts of directories and files without copying data. While they help improve the system organization and efficiency, sometimes you might be required to remove a symlink from a directory to get rid of broken links, alter configurations, or fix access errors.
But removing Symbolic links isn't always that easy. Numerous users encounter "No such file or directory", "Permission Denied", or "Symlink back after removal." This article has a step-by-step guide to the safe removal of symbolic links in Linux, avoiding common pitfalls, and the preservation of the original directory intact.
What is a Symbolic link in Linux?
A symbolic link (symlink) in Linux is an odd file that refers to another file or directory in a shortcut fashion. Symlinks assist in arranging files economically, enabling one to access and control files at different places without the duplication of data.
Also Read: How to Symlink a File in Linux
Types of Symbolic Links in Linux
Symlinks in Linux are of two types, each with a specific purpose:
1. File Symlinks (Symbolic Link to a File)
A file symlink is a soft link pointing to a specific file. It gives direct access to a file from somewhere else without copying or moving it
Example: Creating a Symlink to a File
ln -s /home/user/documents/report.txt /home/user/Desktop/report_link.txt
Note: When the original file (report.txt) is deleted, the symlink remains but is of no use, i.e., it does not point to an existing file anymore.
2. Directory Symlinks (Symbolic Link to a Directory)
A directory symlink makes it easy for users to have a link to a directory as a whole instead of one file. It is particularly helpful while dealing with projects, scripts, and group files.
Example: Creating a Symlink to a Directory
ln -s /var/www/html/project /home/user/Desktop/project_link
Now project_link on the Desktop provides instant access to /var/www/html/project without copying files.
Also Read: Soft and Hard links in Unix/Linux
How to Identify a Symlink in Linux?
To check if a file is a symlink, run:
ls -l
- The
l
at the beginning indicates a symbolic link mydir
is the symlink, and it points to /home/user/data
Also Read: How to Use ln Command to Create Symbolic Links in Linux
Method to Remove a Symbolic link to a Directory in Linux
Method 1: Using the rm
Command
The rm
command is commonly used in Linux to remove files, directories, and delete symbolic links (symlinks).
Syntax:
rm symlink_name
Example: Remove a Symlink to a Directory
rm mydir
Note: This removes only the symlink, NOT the target directory! and Do not use rm -r mydir/
, as this may delete the target directory if it’s not a symlink.
Important Notes:
- DO NOT use rm -r mydir/ unless you first determine that mydir is a symlink.
- If
mydir
is a real directory (not a symbolic link), using rm -r mydir/
WILL DELETE EVERYTHING inside it. - To safely check prior to deletion, verify if mydir is a symlink use the below command:
ls -l mydir # If the output starts with l
, it is a symlink (lrwxrwxrwx
), and rm mydir
is safe to use.
Method 2: Using the unlink
Command
unlink is yet another safe command to remove symbolic links in Linux. Unlike rm, which removes more than one file, unlink removes a single symlinks at a time.
Syntax:
unlink symlink_name
Note: It removes only the symlink and not the target directory.
Why Use unlink
Instead of rm
?
- unlink is a safer command since it removes only symlinks and never removes actual directories or files by mistake.
- If you're working in a delicate system environment, unlink reduces chances of accidental deletion.
Method 3: Using find
to Remove Multiple Symlinks
Use the find
command if you have many symlinks spread across directories and want to delete them all at once
find /path/to/directory -type l -delete # This removes all symlinks inside /path/to/directory
without deleting real files or folders.
Note: This removes only symlinks only and does not touch real files.
Example: Remove All Symlinks in /home/user/
find /home/user/ -type l -delete
How to Use find
More Effectively?
If you want to remove symlinks only in the current working directory (not subdirectories), do:
find . -maxdepth 1 -type l -delete # This prevents recursive removal of symlinks in subdirectories.
How to Check for Broken Symlinks Before Deleting?
Broken symlinks (also called dangling symlinks) point to files or directories that no longer exist. Removing them cleans up your file system.
Find All Broken Symlinks:
Use the below command to find all the broken Symbolic link in the directory:
find /path/to/directory -xtype l
Remove All Broken Symlinks:
When we find all the broken symlinks than remove that by using the below command:
find /path/to/directory -xtype l -delete
Troubleshooting Issues When Removing a Symlink to a Directory
If you might encounter some errors which preventing successful deletionafter using rm
or unlink
to remove a symbolic link, than below are the common issues with solution:
1. “No such file or directory” Error
This error occur when you tried to delete a symlink, but it doesn’t exist it occurs if:
- The symlink might have been removed already.
- The symlink is broken.
- The symlink name might contain spaces or special characters.
Step 1: Verify if the symlink exists
Run the following command to check whether the symlink exists or not:
ls -l
Step 2: Find and delete broken symlinks
To remove only broken symbolic links without touching real files, use the below command:
find . -xtype l -delete # This command searches for symlinks that point to missing files and deletes them.
2. “Operation Not Permitted” Error
You get a "Permission Denied" or "Operation Not Permitted" error when trying to delete a symlink it cause due to:.
- The symlink has restricted permissions.
- The immutable attribute is set, preventing modifications.
- You lack admin (sudo) privileges to delete the symlink.
Step 1: Check and change permissions
Run this command to check the permissions of the symlink:
ls -l mydir
If the output looks like this:
lrwxr--r-- 1 root root 11 Jan 30 12:00 mydir -> /home/user/data # It means only the root user has write access
To change permissions and allow deletion, run:
sudo chmod 777 mydir
Then try deleting it again:
sudo rm mydir
3. Symlink Still Exists After Deletion
In this error you removed the symlink, but it keeps reappearing after deletion
- The symlink is automatically recreated by a running script or process.
- The immutable flag is preventing deletion.
- A scheduled job (
cron job
) or startup script is recreating the symlink.
Step 1: Ensure the symlink isn't recreated by a script
Check if any process is recreating the symlink using:
ps aux | grep mydir
If you see a script or program running, stop it:
sudo systemctl stop <service_name>
Step 2: Check for scheduled jobs recreating the symlink
Sometimes, a cron job may be set up to recreate the symlink at regular intervals. To check:
crontab -l
If a cron job is found that creates the symlink, remove or disable it by running:
crontab -e # Then delete the related cron entry
Step 3: Verify and remove the immutable attribute
If the symlink is still present after deletion, it may have an immutable flag set. Remove it using:
lsattr -d mydir
sudo chattr -i mydir
sudo rm mydir
Also Read:
Conclusion
It is simple to remove a symbolic link to a directory if you know the right commands and are familiar with symlinks. The commands rm, unlink, and find -type l -delete simply remove symlinks with no modifications to the source directories at all. You always need to check the symlink first using ls -l prior to deleting, especially if you don't want accidentally to delete actual files.
If you get permission denied or no such file or directory errors, and you use chmod to verify permissions or chattr to strip away attributes, usually fixes the issue. To delete symlinks, use find with -xtype l to clean them up in a good way.
Similar Reads
How to Remove Directory in Linux
In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files
5 min read
How to Insert a Directory to PATH in Linux
Adding a directory to the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without requiring the full path each time. When you type a command in the Linux terminal, the shell searches for the corresponding executable file to run it. These executables
5 min read
How to Check the Size of a Directory in Linux
Many times during working on Linux we may encounter situations, where we want to check the total size occupied by a directory, especially when working with large files and directories. Knowing the file or directory size can help a user perform tasks like storage allocation, size comparison, etc. At
7 min read
How to Use ln Command to Create Symbolic Links in Linux
A symbolic link (symlink) is like a shortcut that points to a file or folder on Linux and other similar operating systems. Symlinks can be useful for organizing files and folders, or for making it easier to access files from different locations. In this guide, you'll learn how to create symbolic lin
5 min read
How to Copy a Directory in Linux
There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about
5 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help y
7 min read
How to Symlink a File in Linux
In Linux/UNIX, a symbolic link or soft link, also referred to as a symlink, is a useful tool for linking files or directories from different locations. Similar to a pointer in C programming, a symlink directs to the original file from an alternate location. The creation of symbolic links is facilita
4 min read
How to Manage Directories in Linux?
Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
6 min read
How to Rename a Folder in Linux
Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal
12 min read
How to Recover a Deleted File in Linux?
We all have often faced a problem where we have accidentally deleted some files in Linux, that we regretted deleting later on after we did not even find it in the trash. But what if we can recover them? Here, we will discuss How we can recover a Deleted File in Linux.Whenever we delete something fro
4 min read