Remove symbolic links file in Linux?


Overview

Symbolic links allow us to access files more easily, even if they're located in a different location. In this tutorial we'll learn how to remove symbolic links from our system and replace them with the original file.

The Problem

If we have an aDir folder and an aFile.text file under our current working dir, let's say. We've also created two symbolic link files pointing to the folder and the subfolder −

$ ls -l
total 0
drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/
-rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
lrwxrwxrwx 1 kent kent 4 Apr 26 23:48 dirLink -> aDir/
lrwxrwxrwx 1 kent kent 9 Apr 26 23:48 fileLink -> aFile.txt

We want to remove the two soft-linked files.

You can use several different methods to achieve that.

Using the rm Command

The rm (remove) command can remove files and folders. We can use this command to remove symbolic links.

Let's first remove fileLink using the 'rm' (remove) command.

$ rm fileLink
$ ls -l
total 0
drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/
-rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
lrwxrwxrwx 1 kent kent 4 Apr 26 23:48 dirLink -> aDir/

As shown in the output above, we've successfully deleted the symbolic link named Link. The syntax for deleting a symbolic link and its target are the same.

Next, try to remove the dirLink link −

$ rm dirLink/
rm: cannot remove 'dirLink/': Is a directory

The rmdir command refuses to delete the directory and prints an error message instead. The message doesn't look right because we don't have the dirLink directory. What’s not right with the command?

We've added a forward slash after link name.

We should use the −f option when attempting to remove symbolic links using the rm commmand. Regardless of whether the target is a file, a directory, or both, we shouldn't add the / character.

After understanding why the problem occurred, let’s now fix it and see if the problem has been resolved.

$ rm dirLink
$ ls -l
total 0
drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/
-rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt

Excellent! The dirlink link has now been removed.

Using the unlink Command

The unlink command belongs to the CoreUtils package and is available on all Linux distributions.

Let’s recreate the two links and see if we can remove them using the unlinked command.

We're first going to remove the fileLink link.

$ unlink fileLink
$ ls -l
total 0
drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/
-rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
lrwxrwxrwx 1 kent kent 4 Apr 27 00:15 dirLink -> aDir/

It's pretty easy to use, and it works.

Now, we’ll pass dirLink/ to unlink to see whether it can remove the link.

$ unlink dirLink/
unlink: cannot unlink 'dirLink/': Not a directory

If we pass a URL containing a trailing slash, the unlinked command doesn't remove the link either.

Therefore, we should only pass the link name to unlink −

$ unlink dirLink
$ ls -l
total 0
drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/
-rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt

If we pass the name of an ordinary file to the unlinked command, it'll remove the file even though it isn't a linked one.

$ unlink aFile.txt
$ ls -l
total 0
drwxr-xr-x 3 kent kent 60 Apr 27 00:22 ./
drwxrwxrwt 23 root root 840 Apr 27 00:18 ../
drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/

Deleting Multiple Links in One Shot

We can use find | xargs rm combination to delete files in the find command’s result. We can remove symbolic links by following the same steps.

Now, we’ll look at another 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 above indicates that we have multiple links within our current directory and sub-directory. Some links have the name 2BDeleted*, while others have the name keepMe*.

We want to delete all symbolic links whose name matches the regular expression "2bDeleted*.

We should be careful not to remove any symbolic links. For example, if we want to keep the regular file 2BDeleted_I_Am_Not_A_Link.txt intact, then we should not delete the file named 2BDeleted_

Next, see how we delete link file using the find | xargs rm (or unlink) combination.

The first thing we need to do is write a command to find all the URLs we want to remove from our site.

$ find . -type l -name '2bDeleted*'
./2bDeleted.txt
./2bDeleted_01.txt
./2bDeletedDir
./2bDeletedDir_01
./2bDeletedDir_02
./aDir/2bDeleted_etc

We pass two parameter to the find function −

  • type l − Use to search symbolic links file only

  • name ‘2bDeleted*’ − The link name matches for given pattern

Next, we can pipe the output to the xargs command −

$ find . -type l -name '2bDeleted*' | xargs -I{} rm "{}"
$ tree
.
├──   2bDeleted_I_am_not_a_link.txt
├──   aDir
     └──   keepMe_etc -> /etc
├──   aFile.txt
├──   keepMeDir -> aDir
└──   keepMe.txt -> aFile.txt

3 directories, 3 files

As the tree diagram illustrates, the command works as expected.

Conclusion

We've learned that when we want to remove a symbolic link, one option is to use either the rm or the unlink command.

However, we should remember that no matter which commands we use, we should always pass the link name without including a trailing forward slash.

We also discussed how to remove symbolic links from multiple directories at once using an example.

Updated on: 26-Dec-2022

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements