How to Remove Symbolic Links in Linux

Last updated: March 15, 2022

A symbolic link, also known as a soft link or symlink, is a link that points to a different file or directory. The symbolic link does not contain any data, but you can perform all operations on the symbolic link file.

Removing a symbolic link does not delete the original file, but deleting a file makes the symlink a dangling link. In this guide, we will learn how to remove symbolic links using unlink and rm commands.

Prerequisites

  • A working Linux computer.
  • Familiarity with common Linux commands.
  • Write permission on the directory in which the symbolic link is stored.

How to Remove a Symbolic Link

When you create a symlink, a link file with the specified name is created on your Linux computer. Deleting a symlink is similar to deleting a file. Linux provides unlink and rm commands to delete symlinks.

Rm Command

Rm is a terminal command to delete the specified file including symbolic links. Generally, this command does not return any output, but you can use the -i option to get a prompt before deleting a file.

Unlink Command

Unlink deletes a name from the Linux file system. If the reference name is a symbolic link then it deletes the link.

Unlink can delete only a single symbolic link in a single command. To delete multiple symbolic links, repeat the unlink command for each file.

Before deleting a symbolic link, you must ensure that it is not used in any function. If you delete a symbolic link that is part of a function, then the function will not work.

Examples to Remove Symbolic Links

Removing a symbolic link is easy and it does not delete the original (target) file. The following examples show you how to delete file and directory symbolic links using unlink and rm commands.

Delete Symbolic Link Using Unlink

To delete a file symlink using the unlink command, type the unlink command followed by the file symbolic link:

unlink symlink_name

You can use the absolute path to symlink_name. Be careful using paths, the wrong path can accidentally delete a target file instead of symlink.

For example:

unlink document.txt
delete symlink using unlink

You can similarly delete a symlink to a directory:

unlink photos
delete symlink to a directory using unlink

Delete Symbolic Link using Rm

The rm command is used to delete files and directories from your Linux computer. A symlink is the same as a file and can be deleted using the rm command as follows:

rm userdata.txt
delete symlink using rm

To delete multiple Linux symbolic links using a single rm command, type:

rm symlink1 symlink2 symlink3

You can also instruct the rm command to prompt you before deleting a symlink:

rm -i apis.txt
delete symlink interactive

To delete a symbolic link directory using the rm command, type:

rm doc
delete symlink to a directory using rm

Do not put a trailing slash (/) at the end of the directory symlink name, otherwise, you will get an error. This usually happens when you use the tab key to complete the symlink name, instead of manually typing it.

Find and Delete Symbolic Links

When removing symbolic links using unlink and rm commands, you must know the symbolic link names and location. If you do not know their names and location, then you cannot delete them using either of these commands.

In such a case, the find command comes to your rescue. You can use the find command to find and delete all symbolic links in a single command. The find command can list both live and broken symlink.

To list symbolic links in the specified directory and its subdirectories using the find command, type:

find /path_to_directory/ -type l

For example, to search for symbolic links in the current directory and its subdirectories:

find . –type l
find symbolic links

The output just shows the symlink names, not the symbolic links pointing files or directories. Now, we can execute the delete command on the results of the find command:

find . -type l -delete
find and delete symbolic links

You can also use the find command to search and delete broken symbolic links:

find . -xtype l -delete
find and delete broken symbolic links

The broken symbolic links are listed in the red color font when we list them using the ls -l command.

Conclusion

In this guide, we learned about different ways to delete symbolic links in Linux. Generally, we use the unlink command to delete symbolic links. You must ensure that the symbolic link is not part of any application or function before deleting it.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment