[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 16, 2023

How to Remove (Delete) Symbolic Links.

How To Remove (Delete) Symbolic Links.

In this guide, we will show you how to remove (delete) symbolic links in Linux/UNIX systems using the rm, unlink, and find commands.

A symbolic link, also known as a symlink, is a special type of file that points to another file or directory. It is something like a shortcut in Windows. A symlink can point to a file or a directory on the same or a different filesystem or partition.

 

Before You Begin

To remove a symlink, you need to have writing permissions on the directory that contains the symlink. Otherwise, you will get “Operation not permitted” error.

When you remove a symlink, the file it points to is not affected.

Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to.

ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Apr 16  2018 /usr/bin/python -> python2.7

The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.

 

Remove Symbolic Links with rm

The rm command removes given files and directories.

To delete a symlink, invoke the rm command followed by the symbolic link name as an argument:

rm symlink_name

On success, the command exits with zero and displays no output.

With rm you can delete more than one symbolic links at once. To do that pass the names of the symlinks as arguments, separated by space:

rm symlink1 symlink2

To get prompted before removing the symlink, use the -i option:

rm -i symlink_name

To confirm type y and press Enter.

rm: remove symbolic link 'symlink_name'? 

If the symbolic link points to a directory, do not append the / trailing slash at the end. Otherwise, you will get an error:

rm symlink_to_dir/
rm: cannot remove 'symlink_to_dir/': Is a directory

If the name of the argument ends with /, the rm command assumes that the file is a directory. The error happens because, when used without the -d or -r option, rm cannot delete directories.

To be on the safe side, never -r option when removing symbolic links with rm. For example, if you type:

rm -f symlink_to_dir/

The contents of the target directory will be deleted.

 

The unlink command deletes a given file. Unlike rm, unlink accepts only a single argument.

To delete a symbolic link, run the unlink command followed by the symlink name as an argument:

unlink symlink_name

If the command executes successfully, it displays no output.

Do not append the / trailing slash at the end of the symlink name because unlink cannot remove directories.

 

If you delete or move the source file to a different location, the symbolic file will be left dangling (broken).

To find all broken symbolic links under a given directory, run the following command:

find /path/to/directory -xtype l
/path/to/directory/symlink1
/path/to/directory/subdir/symlink2

The command will list all broken links under the directory and its subdirectories.

If you want to exclude the symlinks that are contained in the subdirectories pass the -maxdepth 1 option to find :

find /path/to/directory -maxdepth 1 -xtype l
/path/to/directory/symlink1

Once you find the broken symlinks, you can either manually remove them with rm or unlink or use the -delete option of the find command:

find /path/to/directory -xtype l -delete

 

Conclusion

To remove a symbolic link, use either the rm or unlink command followed by the name of the symlink as an argument. When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.

Keep reading
How to Add a Directory to PATH in Linux
April 12, 2023

How to Add a Directory to PATH in Linux

How To Add A Directory To PATH In Linux This article shows how to add directories to your $PATH in Linux systems. When you type a command on the command line, you’re basically telling the shell to run an executable file with the given name. In Linux, these executable programs like ls , find ,…

Read article
How to use ps command.
April 12, 2023

How to use ps command.

How To Use Ps Command. This article explains how to use the ps command to list the currently running processes and display information about those processes. In Linux, a running instance of a program is called process. Occasionally, when working on a Linux machine, you may need to find out what…

Read article
How to understand OSI.
April 11, 2023

How to understand OSI.

OSI Model OSI stands for Open System Interconnection is a reference model that describes how information from a software application in one computer moves through a physical medium to the software application in another computer. OSI consists of seven layers, and each layer performs a particular…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00