- How to Remove (Delete) Symbolic Links in Linux
- Before You Begin #
- Remove Symbolic Links with rm #
- Remove Symbolic Links with unlink #
- Find and Delete Broken Symbolic Links #
- Conclusion #
- Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link
- Difference Between a Soft Link and a Hard Link
- How to Create a Symlink
- How to Create a Symlink for a File – Example Command
- How to Create a Symlink for a Folder – Example Command
- How to remove a symlink
- How to Use Unlink to Remove a Symlink
- How to use rm to Remove a Symlink
- How to Find and Delete Broken Links
- Wrapping up
- How Do I Remove a Symbolic Link in Linux?
- Remove a Symbolic Link using the unlink command
- Remove a Symbolic Link using the rm command
- About the author
- Karim Buzdar
How to Remove (Delete) Symbolic Links in Linux
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.
In this guide, we will show you how to remove (delete) symbolic links in Linux/UNIX systems using the rm , unlink , and find commands.
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.
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:
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:
To get prompted before removing the symlink, use the -i option:
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: 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:
The contents of the target directory will be deleted.
Remove Symbolic Links with unlink #
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:
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.
Find and Delete Broken Symbolic Links #
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
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.
If you have any questions or feedback, feel free to leave a comment.
Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link
Dillion Megida
A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows.
Some people call symlinks «soft links» – a type of link in Linux/UNIX systems – as opposed to «hard links.»
Difference Between a Soft Link and a Hard Link
Soft links are similar to shortcuts, and can point to another file or directory in any file system.
Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.
Let’s look at the steps involved in creating and removing a symlink. We’ll also see what broken links are, and how to delete them.
How to Create a Symlink
The syntax for creating a symlink is:
ln is the link command. The -s flag specifies that the link should be soft. -s can also be entered as -symbolic .
By default, ln command creates hard links. The next argument is path to the file (or folder) that you want to link. (That is, the file or folder you want to create a shortcut for.)
And the last argument is the path to link itself (the shortcut).
How to Create a Symlink for a File – Example Command
ln -s /home/james/transactions.txt trans.txt
After running this command, you will be able to access the /home/james/transactions.txt with trans.txt . Any modification to trans.txt will also be reflected in the original file.
Note that this command above would create the link file trans.txt in your current directory. You can as well create a linked file in a folder link this:
ln -s /home/james/transactions.txt my-stuffs/trans.txt
There must be a directory already called «my-stuffs» in your current directory – if not the command will throw an error.
How to Create a Symlink for a Folder – Example Command
This would create a symlinked folder called ‘james’ which would contain the contents of /home/james . Any changes to this linked folder will also affect the original folder.
How to remove a symlink
Before you’d want to remove a symlink, you may want to confirm that a file or folder is a symlink, so that you do not tamper with your files.
Running this command on your terminal will display the properties of the file. In the result, if the first character is a small letter L (‘l’), it means the file/folder is a symlink.
You’d also see an arrow (->) at the end indicating the file/folder the simlink is pointing to.
There are two methods to remove a symlink:
How to Use Unlink to Remove a Symlink
This deletes the symlink if the process is successful.
Even if the symlink is in the form of a folder, do not append ‘/’, because Linux will assume it’s a directory and unlink can’t delete directories.
How to use rm to Remove a Symlink
As we’ve seen, a symlink is just another file or folder pointing to an original file or folder. To remove that relationship, you can remove the linked file.
Note that trying to do rm james/ would result an error, because Linux will assume ‘james/’ is a directory, which would require other options like r and f . But that’s not what we want. A symlink may be a folder, but we are only concerned with the name.
The main benefit of rm over unlink is that you can remove multiple symlinks at once, like you can with files.
How to Find and Delete Broken Links
Broken links occur when the file or folder that a symlink points to changes path or is deleted.
For example, if ‘transactions.txt’ moves from /home/james to /home/james/personal , the ‘trans.txt’ link becomes broken. Every attempt to access to the file will result in a ‘No such file or directory’ error. This is because the link has no contents of its own.
When you discover broken links, you can easily delete the file. An easy way to find broken symlinks is:
This will list all broken symlinks in the james directory – from files to directories to sub-directories.
Passing the -delete option will delete them like so:
find /home/james -xtype l -delete
Wrapping up
Symbolic link are an interesting feature of Linux and UNIX systems.
You can create easily accessible symlinks to refer to a file or folder that would otherwise not be convenient to access. With some practice, you will understand how these work on an intuitive level, and they will make you much more efficient at managing file systems.
Dillion Megida
Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How Do I Remove a Symbolic Link in Linux?
A symbolic link, also known as symlink, is a file that points to another file. The file points to can be in the same or different directory. It is similar to the shortcuts in Windows OS.
In today’s post, we will be describing how to remove a symbolic link in Linux. Note that removing a symbolic link does not affect the file it points to.
Before removing a file, you can verify whether it is a symbolic link using the ls -l command. It will also show you the file or directory that it points to.
The l in permissions (lrwxrwxrwx) confirms that it’s a symbolic link.
Remove a Symbolic Link using the unlink command
The unlink command is used for removing a single file from the file system. To remove a symbolic link in Linux, type the unlink command followed by the name of the symbolic link and hit Enter:
Replace symbolic_link with the name of the symbolic link you want to remove. After that, you can use the ls -l command to confirm if the symlink has been removed.
Remove a symlink that points to a directory, don’t use the slash after the directory name. Let’ say we want to remove a symbolic link directory named Docs, as shown in the following screenshot:
The command to remove the symlink directory will be:
Remove a Symbolic Link using the rm command
The rm command can also be used to remove a symbolic link. For removing a symbolic link in Linux, type the rm command followed by the name of the symbolic link and hit Enter:
After that, you can use the ls -l command to confirm if the symlink has been removed.
You can also use the -i flag with the rm command to prompt for confirmation.
Removing a symlink that points to a directory, don’t use the slash after the directory name. Let’ say we want to remove a symbolic link directory named Docs, as shown in the following screenshot:
The command to remove the symlink directory will be:
After that, you can use the ls -l command to confirm if the symlink has been removed.
That is all there is to it! You have learned to remove a symbolic link in Linux OS using the unlink and rm command in this post. While removing a symbolic link, make sure only to remove the symbolic link itself, not the file or directory it is linking to.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.