- Remove a symlink to a directory
- 12 Answers 12
- How to Remove Symbolic Links
- 1. Overview
- 2. Introduction to the Problem
- 3. Using the rm Command
- 4. Using the unlink Command
- 5. Deleting Multiple Links in One Shot
- 6. Conclusion
- 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
Remove a symlink to a directory
I have a symlink to an important directory. I want to get rid of that symlink, while keeping the directory behind it. I tried rm and get back rm: cannot remove ‘foo’ .
I tried rmdir and got back rmdir: failed to remove ‘foo’: Directory not empty
I then progressed through rm -f , rm -rf and sudo rm -rf Then I went to find my back-ups. Is there a way to get rid of the symlink with out throwing away the baby with the bathwater?
what’s wrong with asking linux-related questions? last time i checked, stack overflow was not os-specific
@mwfearnley It would be, but check the date. Super User wasn’t a thing yet back then. It’s been ported, so it’s there too. Not sure why it’s still here, but it’s my highest scoring question and I’m not gonna look gift karma in the mouth.
12 Answers 12
# this works: rm foo # versus this, which doesn't: rm foo/
Basically, you need to tell it to delete a file, not delete a directory. I believe the difference between rm and rmdir exists because of differences in the way the C library treats each.
At any rate, the first should work, while the second should complain about foo being a directory.
If it doesn’t work as above, then check your permissions. You need write permission to the containing directory to remove files.
This does not always work. Occasionally you need to run rm -rf folderName (without trailing /) in order to remove the symlink. Amazon Linux behaves this way under certain circumstances, for example.
@r3mus: Never use -r with a symlink unless you want to lose everything inside it. -f shouldn’t be needed either, except perhaps to override file permissions.
Specifically, unlink has no relation to the ln AKA ‘link’ operation. The name unlink refers to the process of unlinking/removing a file from the file system’s file table so that the contents become detached from any reference to them — they are unlinked. It’s a confusing name that’s downright misleading when applied to links and symlinks in particular. unlink will work with symlinks because it works with any file regardless of type.
If you’re ever actually worried about doing something silly, alias rm=»rm -i» has saved more hides than just about anything else out there I think.
use the «unlink» command and make sure not to have the / at the end
unlink() deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse. If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.
I think this may be problematic if I’m reading it correctly.
If the name referred to a symbolic link the link is removed.
If the name referred to a socket, fifo, or device the name for it is removed but processes that have the object open may continue to use it.
How to Remove Symbolic Links
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Overview
Creating symbolic links allows us to access files more flexibly, even if the target files are in a different file system.
In this tutorial, we’ll have a look at how to remove a symbolic link.
2. Introduction to the Problem
Let’s say we have an aDir directory and an aFile.txt file under the current working directory. Also, we’ve created two symbolic links pointing to the file and the directory:
$ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt lrwxrwxrwx 1 kent kent 4 Apr 26 23:48 dirLink -> aDir/ lrwxrwxrwx 1 kent kent 9 Apr 26 23:48 fileLink -> aFile.txt
Our goal is to remove the two soft links.
There are several ways to achieve that. Next, let’s see them in detail.
3. Using the rm Command
We know the rm command can delete files and directories. Additionally, we can use this command to delete symbolic links.
First, let’s remove fileLink using the rm command:
$ rm fileLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt lrwxrwxrwx 1 kent kent 4 Apr 26 23:48 dirLink -> aDir/
As the output above shows, We have successfully deleted fileLink. The syntaxes of deleting a symbolic link and a file are the same.
Next, let’s remove the dirLink link:
$ rm dirLink/ rm: cannot remove 'dirLink/': Is a directory
Oops! The rm command refuses to remove the link and prints an error message. The message looks weird since we don’t have the dirLink directory at all. What’s wrong with the command?
The problem is, we’ve added a forward slash after the link name.
When we attempt to delete symbolic links using the rm command, we should pass the link name to rm. No matter if the target is a file or a directory, we shouldn’t include the slash.
Now that we understand the cause of the problem, let’s fix it and try the command again:
$ rm dirLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
Great! The dirLink link has now been removed.
4. Using the unlink Command
The unlink command is a member of the CoreUtils package, and it’s available on all Linux distros.
Let’s re-create the two links and try to delete them using the unlink command.
First, we’re going to delete the fileLink link:
$ unlink fileLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt lrwxrwxrwx 1 kent kent 4 Apr 27 00:15 dirLink -> aDir/
The command is pretty straightforward, and it works.
Now, let’s try passing “dirLink/” to unlink to see if it can remove the link:
$ unlink dirLink/ unlink: cannot unlink 'dirLink/': Not a directory
Similarly, if we pass a name ending with a slash, the unlink command refuses to remove the link, too.
Therefore, we should only pass the link name to unlink:
$ unlink dirLink $ ls -l total 0 drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/ -rw-r--r-- 1 kent kent 0 Apr 26 23:46 aFile.txt
It’s worth mentioning that if we pass the name of a regular file to the unlink command, it’ll delete the file, even though it’s not a link:
$ unlink aFile.txt $ ls -l total 0 drwxr-xr-x 3 kent kent 60 Apr 27 00:22 ./ drwxrwxrwt 23 root root 840 Apr 27 00:18 ../ drwxr-xr-x 2 kent kent 40 Apr 26 23:48 aDir/
5. Deleting Multiple Links in One Shot
We know that we can use the find | xargs rm combination to delete files in the find command’s result. Similarly, we can remove symbolic links using the same technique.
Now, let’s see another example:
$ tree . ├── 2bDeleted_01.txt -> aFile.txt ├── 2bDeletedDir -> aDir ├── 2bDeletedDir_01 -> aDir ├── 2bDeletedDir_02 -> aDir ├── 2bDeleted_I_am_not_a_link.txt ├── 2bDeleted.txt -> aFile.txt ├── aDir │ ├── 2bDeleted_etc -> /etc │ └── keepMe_etc -> /etc ├── aFile.txt ├── keepMeDir -> aDir └── keepMe.txt -> aFile.txt 7 directories, 5 files
The output above shows that we have multiple links in the current directory and the sub-directory “aDir”. Also, some link names are with the pattern “2bDeleted*”, while other link names follow the pattern “keepMe*“.
Our goal is to recursively remove all symbolic links whose names match the pattern “2bDeleted*“.
We should be careful that we only want to remove symbolic links. For example, we should keep the regular file 2bDeleted_I_am_not_a_link.txt intact, even though its name begins with “2bDeleted“.
Next, let’s see how we achieve the goal using the find | xargs rm (or unlink) combination.
The first step is to build the find command to locate all the links we want to delete:
$ find . -type l -name '2bDeleted*' ./2bDeleted.txt ./2bDeleted_01.txt ./2bDeletedDir ./2bDeletedDir_01 ./2bDeletedDir_02 ./aDir/2bDeleted_etc
We pass two expressions to the find command:
- -type l: We search symbolic links only
- -name ‘2bDeleted*’: The link name matches the given pattern
Next, we can pipe the result to the xargs command:
$ find . -type l -name '2bDeleted*' | xargs -I<> rm "<>" $ tree . ├── 2bDeleted_I_am_not_a_link.txt ├── aDir │ └── keepMe_etc -> /etc ├── aFile.txt ├── keepMeDir -> aDir └── keepMe.txt -> aFile.txt 3 directories, 3 files
As the tree output shows, the command works as we expected.
6. Conclusion
In this article, we’ve learned that when we want to remove a symbolic link, we can consider picking the rm or the unlink command.
However, we should keep in mind that no matter which command we use, we should only pass the link names and not include a trailing forward slash.
Additionally, we addressed how to recursively delete symbolic links in one shot through an example.
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.