How To Remove (Delete) Symbolic Links In Linux?

A symbolic link is a file that redirects into another file or folder. Symbolic links also called shortcuts in Windows operating system. Symbolic links provide the ability to put a single file or directory into multiple locations without copying. Symbolic links do not contain actual data where only provides a link to the actual data or directory.

List Symbolic Links

The ls command can be used to list symbolic links. The -l option shuld be provided into the ls command which wil list detailed attributes about files. In the following example the “mylink” is a link into the file named “myfile.txt”.

ls -l

The output is like below. The l character express that this is a link. Also “mylink -> myfile.txt” express that “mylink” is a link to the “myfile.txt”.

lrwxrwxrwx 1 ismail ismail       10 Ara 26 17:05 mylink -> myfile.txt

Remove (Delete) Symbolic Link with rm Command

The rm command is used to remove files and folders. But the rm command can be also used to remove symbolic links. Just providing the link name removes the symbolic link. In the following example we will remove the symbolic link “mylink”.

rm mylink

Also multiple symbolic links can be removed with the rm command. The link names are provided by separating them with spaces.

rm mylink yourlink alllink

A link can be also removed by providing its full or absolute path. In the following example we will remove the link named mylink which is located in /home/ismail/.

rm /home/ismail/mylink

To prevent errors before removing (deleting) symbolic links we can use the confirmation. The -i option can be used to confirm the removal of the symbolic links.

rm -i mylink

Remove (Delete) Symbolic Link with unlink Command

The unlink command is specifically created to unlink or delete a link. The unlink command is described as “call the unlink function to remove the specified file” . Differently the unlink command can remove a single link at a time.

unlink mylink

If the symbolic link is deleted succesfully there will be no output.

Remove (Delete) Symbolic Link with find Command

The find command is used to find different types of files and directories. Also, the find command can execute commands on results. We can use the find command in order to find links in the specified path or with the specified name and remove or delete them. In the following example

find /home/ismail -type l -delete

We can also specify pattern for the symbolic name we want to delete by using the find command. In the following example we will delete symbolic links which names start with “my”.

find /home/ismail -type l -delete -name my*

Remove (Delete) Symbolic Link with File Manager (GUI)

File manager is a GUI tool used with different Desktop environments in Linux. File managers can list and delete symbolic links easily. The symbolic links listed with a arrow icon or similar. The symbolic link can be deleted by right click on it and select “Move to Trash” or “Delete” etc.

Remove (Delete) Symbolic Link with File Manager (GUI)

Find and Remove (Delete) Broken Symbolic Links

Symbolic links are created to point to a file or directory. but when the pointed file or directory is deleted or moved into a different path the symbolic links pointing became broken. Simply broken symbolic links targets do not exist. The find command is used to find specified files or directories. Also, the find command can find links. We can specify the criteria as broken links to find and delete with the -delete option to remove broken links. We will use the -xtype option to specify the broken type of file where l character is used to set a symbolic link.

find /home/ismail -xtype l -delete

Leave a Comment