- Remove a symlink to a directory
- 12 Answers 12
- Remove all symbolic links of files in one command
- 2 Answers 2
- Remove all symlinks in a folder
- 4 Answers 4
- Delete all broken symbolic links with a line?
- 9 Answers 9
- How to Remove Symbolic Links in Linux
- Prerequisites
- How to Remove a Symbolic Link
- Rm Command
- Unlink Command
- Examples to Remove Symbolic Links
- Delete Symbolic Link Using Unlink
- Delete Symbolic Link using Rm
- Find and Delete Symbolic Links
- Conclusion
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.
Remove all symbolic links of files in one command
I want to remove all the symbolic links of files. I have a directory structure like /usr/local/instantclient/11.2.0.3 which contains lots of files and i have symbolic links of these files in /usr/local/lib/ Now i want to delete all these symbolic links of those files. How can i do this in one command. If i remove the actual dir /usr/local/instantclient/11.2.0.3 containing files then it will leave the broken links in /usr/local/lib .
2 Answers 2
To remove the links (from man find under -type ):
l symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
I think this should do the trick:
find /usr/local/lib/ -maxdepth 1 -follow -type l
Does the output produce a list of the files you want to delete? If so, when you are 100% sure:
find /usr/local/lib/ -maxdepth 1 -follow -type l -delete
This will remove only broken links. To delete all links, remove the -follow stanza, but I wouldn’t do that under /usr/local/lib .
Just to clarify: this assumes that the target directory has already been removed, so the links are already broken. The command will list all broken symlinks in the /usr/local/lib directory (but not recurse), so if there are other broken links, then it will show more than needed. Not that there should be a lot of broken links there to begin with, so it’s probably OK, but it is possible for a system to have purposefully broken links, and we’d like to spare those.
I tried the command suggested by Petter. The -follow option doesn’t work. Only find /usr/local/lib/ -maxdepth 1 -type l | delete worked. But i think this is not the best way to do it. There is actually no way to list all the soft links of a file while hard links of a files can be listed using inode value of file. Here is a discussion about it stackoverflow.com/questions/4532241/…
How to find and delete all hard links to a file is discussed here linuxcommando.blogspot.in/2008/09/… but this doesn’t work for sybmolic links.
While there may be no (easy) way of «reverse readlink» a symbolic link, I don’t see how that is relevant to your problem; you said that the links created were located in /usr/local/lib ? also, do you have no -follow option in your version of find?
Remove all symlinks in a folder
How do I remove all symlinks in a folder (dozens of them) at once? It’s not practical to insert every single one of them by hand when using unlink or rm.
What do you mean by «folder»? Are you just talking about what Linux calls directories, or do you have some other usage in mind?
4 Answers 4
You can use the find -command to do that:
find /path/to/directory -maxdepth 1 -type l -delete
To be on the safe side, check first without the -delete -option:
find /path/to/directory -maxdepth 1 -type l
-maxdepth 1 ensures that find will look only in /path/to/directory but not in it’s subfolders for symlinks. Feel free to take a look at man find .
List the links in the current directory alias folder and check that you really want to remove them,
find -type l -ls # search also in subdirectories find -maxdepth 1 -type l -ls # search only in the directory itself
If things look good, and you want to delete these links, run
find -type l -delete # delete also in subdirectories find -maxdepth 1 -type l -delete # delete only in the directory itself
If you want to delete interactively, you can use the following command line (this is safer)
find -type l -exec rm -i <> + # delete also in subdirectories find -maxdepth 1 -type l -exec rm -i <> + # delete only in the directory itself
Delete all broken symbolic links with a line?
For a given folder, how can I delete all broken links within it? I found this answer that shows how to delete one broken link, but I can’t put that together in only one line. Is there a one-liner for this? A broken symbolic is a link that points to a file/folder that doesn’t exists any longer.
Straight from «man find»: ` find -L /usr/ports/packages -type l -exec rm — <> + Delete all broken symbolic links in /usr/ports/packages.`
9 Answers 9
Here’s a POSIX way of deleting all broken symbolic links in the current directory, without recursion. It works by telling find to traverse symbolic links ( -L ), but stopping ( -prune ) at every directory-or-symbolic-link-to-such.
find -L . -name . -o -type d -prune -o -type l -exec rm <> +
You can also use a shell loop. The test -L matches symbolic links, and -e matches existing files (excluding broken symlinks).
for x in * .[!.]* . *; do if [ -L "$x" ] && ! [ -e "$x" ]; then rm -- "$x"; fi; done
If you want to recurse into subdirectories, this technique doesn’t work. With GNU find (as found on non-embedded Linux and Cygwin), you can use the -xtype predicate to detect broken symbolic links ( -xtype uses the type of the target for symbolic links, and reports l for broken links).
POSIXly, you need to combine two tools. You can use find -type l -exec … to invoke a command on each symbolic link, and [ -e «$x» ] to test whether that link is non-broken.
find . -type l -exec sh -c 'for x; do [ -e "$x" ] || rm "$x"; done' _ <> +
The simplest solution is to use zsh. To delete all broken symbolic links in the current directory:
The characters in parentheses are glob qualifiers: — to dereference symlinks, @ to match only symlinks (the combination -@ means broken symlinks only), and D to match dot files. To recurse into subdirectories, make that:
How to Remove Symbolic Links in Linux
A symbolic link, also known as a soft link or symlink, is a link that points to a different file or directory. The symbolic link does not contain any data, but you can perform all operations on the symbolic link file.
Removing a symbolic link does not delete the original file, but deleting a file makes the symlink a dangling link. In this guide, we will learn how to remove symbolic links using unlink and rm commands.
Prerequisites
- A working Linux computer.
- Familiarity with common Linux commands.
- Write permission on the directory in which the symbolic link is stored.
How to Remove a Symbolic Link
When you create a symlink, a link file with the specified name is created on your Linux computer. Deleting a symlink is similar to deleting a file. Linux provides unlink and rm commands to delete symlinks.
Rm Command
Rm is a terminal command to delete the specified file including symbolic links. Generally, this command does not return any output, but you can use the -i option to get a prompt before deleting a file.
Unlink Command
Unlink deletes a name from the Linux file system. If the reference name is a symbolic link then it deletes the link.
Unlink can delete only a single symbolic link in a single command. To delete multiple symbolic links, repeat the unlink command for each file.
Before deleting a symbolic link, you must ensure that it is not used in any function. If you delete a symbolic link that is part of a function, then the function will not work.
Examples to Remove Symbolic Links
Removing a symbolic link is easy and it does not delete the original (target) file. The following examples show you how to delete file and directory symbolic links using unlink and rm commands.
Delete Symbolic Link Using Unlink
To delete a file symlink using the unlink command, type the unlink command followed by the file symbolic link:
You can use the absolute path to symlink_name. Be careful using paths, the wrong path can accidentally delete a target file instead of symlink.
You can similarly delete a symlink to a directory:
Delete Symbolic Link using Rm
The rm command is used to delete files and directories from your Linux computer. A symlink is the same as a file and can be deleted using the rm command as follows:
To delete multiple Linux symbolic links using a single rm command, type:
rm symlink1 symlink2 symlink3
You can also instruct the rm command to prompt you before deleting a symlink:
To delete a symbolic link directory using the rm command, type:
Do not put a trailing slash (/) at the end of the directory symlink name, otherwise, you will get an error. This usually happens when you use the tab key to complete the symlink name, instead of manually typing it.
Find and Delete Symbolic Links
When removing symbolic links using unlink and rm commands, you must know the symbolic link names and location. If you do not know their names and location, then you cannot delete them using either of these commands.
In such a case, the find command comes to your rescue. You can use the find command to find and delete all symbolic links in a single command. The find command can list both live and broken symlink.
To list symbolic links in the specified directory and its subdirectories using the find command, type:
find /path_to_directory/ -type l
For example, to search for symbolic links in the current directory and its subdirectories:
The output just shows the symlink names, not the symbolic links pointing files or directories. Now, we can execute the delete command on the results of the find command:
You can also use the find command to search and delete broken symbolic links:
The broken symbolic links are listed in the red color font when we list them using the ls -l command.
Conclusion
In this guide, we learned about different ways to delete symbolic links in Linux. Generally, we use the unlink command to delete symbolic links. You must ensure that the symbolic link is not part of any application or function before deleting it.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks