Open In App

Remove a Symbolic link to a Directory in Linux

Last Updated : 29 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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
start
  • 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.


Next Article
Article Tags :

Similar Reads