how to delete symbolic links in linux

Symbolic links and symlinks are one of the most popular features of posix based Linux systems. The symbolic links gives you the ability to duplicate files in the system with different names and location with out the need to create copies of the file.

There are two different types of links that you can have in Linux: hard links and soft links. There are some major differences between the two types of links. But when talking about deleting a symbolic link, it is very likely that you are talking about deletion of a soft symbolic link.

Deleting a symbolic link is just as easy as creating a symbolic link or copying a symbolic link. This is because in Linux, links are nothing but files and works more or less like files. That means almost all operations that you can perform on a file can be done on a symbolic link as well.

There are essentially two commands that are used to delete or remove a symbolic link: the rm command and the unlink command.

delete symbolic link or soft symlink to a file

using rm command

The rm command is used to delete files and folders in linux. As links are files, the same command can be used to remove links as well. The command takes multiple file names as command line arguments. You can mix and match files and links in the same command as well.

bash$ rm link_one link_two file_one link_three

By default, the command will not output any messages unless there is an error. All the command line options of the rm command is applicable in this case as well. You can use the -i or –interactive option to prompt before every removal.

using unlink command

I mostly use rm command to remove symbolic links and never the unlink command. But unlink is another command that can be used to remove links. The unlink command takes only one command line argument which is the link name.

bash$ unlink link_one

The unlink command is less powerful than the rm command and therein lies its advantage. It is much more advisable to use unlink command from with in shell scripts than the rm command. This will make sure that files and folders are not accidently deleted when running scripts.

As you should already be aware, the directories or folders in linux and nothing but files. That means you can use essentially the same commands as above to delete links to folders. However, there is a caveat that you should keep in mind when the link points to a folder.

The link name is the name of the link without the trailing forward slash. If you specify the forward slash then it refers to the actual folder and not the link itself. That means it will try to delete the actual contents of the folder rather than the link itself. In most cases, this should throw an error unless you have specified the -r or –recursive and -f or –force command line option with the rm command.

delete hard symbolic link to a file

As we mentioned the hard symbolic link earlier in the post, let’s see how you would delete a hard symbolic link. For most practical purposes, hard links are pretty transparent to file operations even more so than soft links. That means you just treat the hard links just as a file, and delete them just the same way as the files or soft links.

bash$ rm -i hard_link_one file_one

Using find command to delete all symbolic links in a directory

If you want to delete all symbolic links in a folder and its sub-folders, then you can use the find command. You can use it to first find all the symbolic links and then to delete it.

bash$ find /path/to/folder -type l -delete
  • -type: the type of file. l represents the link
  • -delete: the delete file action

Common Mistakes

There are some common precautions you should have when deleting links and probably deleting files and folders in general. This will avoid accidently deleting the wrong files.

  • If running from command line, always use the -i option. Actually, you can create a bash alias so that it is the default and override it only when you need.
  • Do not use the forward slash at the end of file and folder names. In the case of links pointing to a folder, it will reference the actual folder and not the symbolic link.
  • Use the rm command sparingly and wisely in the shell scripts. It is very easy to get the arguments wrong and delete the wrong files.