Remove a symlink to a directory [closed]
Asked Answered
C

10

1195

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 without throwing away the baby with the bathwater?

Candancecandela answered 16/10, 2008 at 20:18 Comment(6)
TIP: rm -r link/ deletes the content on the targetHanus
After going through the exact same trouble of finding my backup, I reached here to figure out the solution for future. The way you phrased the question at the end made me smile - with out throwing away the baby with the bathwaterPolanco
Related: Safely remove a symlink or hardlinkCourtesy
From Remove Symlink to home directory: "If you have just created a symlink, you can simply rm it. Without the -r and -f it will not delete directories"Courtesy
@Peter Mortensen This was a genuine question at the time, not a joke. The first problem was not -r or -f. That would not have helped me. The problem was rm foo vs rm foo/. Tab completion in bash will add the slash. That slash makes rm try to treat it as a directory. Without -r or -f it will fail, then the inexperienced will google the error message and be told to use -r. And loose data. Neither of the proposed related questions explain the / issue. As far as I can tell only the accepted answer here has that explanation, on the whole internet.Candancecandela
@Peter Mortensen That said, I have no objections to closing this It has more then enough answers, only one of which is correct. But I would object to removing it until the / problem was better documented somewhere.Candancecandela
S
1462
# 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.

Sidewinder answered 16/10, 2008 at 20:22 Comment(11)
ah yes. that makes sense. I never typed foo, I typed f<tab> and bash filled in a / for me.Candancecandela
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.Mccary
@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.Sidewinder
provided you use it without the trailing slash /, I've encountered a few strange circumstances where this is required. Admittedly it's always best to make a backup of the content before attempting though, just in case ;)Mccary
@fxfilmxf unlink may not be available on some platforms where rm is.Coconut
@fxfilmxf (or someone else) why would you say unlink is 'better' ?Fortissimo
Update: the comments in the answer below on 'unlink' explain very well the pros/cons of unlink vs rm (https://mcmap.net/q/45535/-remove-a-symlink-to-a-directory-closed)Fortissimo
@Fortissimo I still don't get how it's better. It doesn't handle unlink foo/ either (which would make it better - handling a common accidental error) and as noted by others, rm foo/ will fail loudly unless you intentionally add the -r flag which will do... bad things. But only because you told it to. The fact there's no recursive version of unlink (usually - see the note about OS X's version of unlink) doesn't make it better, it makes it worse IMO.Sidewinder
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.Sidewinder
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.Sidewinder
What is happening with say rm -rf foo/ to make it delete the contents of the target? If the symbolic link is a file, shouldn't it just be treated like rm readme.txt/?Woald
M
864

Use the unlink command and make sure not to have the / at the end:

unlink mySymLink

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.

Mouse answered 16/10, 2008 at 20:22 Comment(10)
@OmarAbid The "rm foo/" version scares me. This one seems a lot safer :)Mouse
I think unlink is better for removing a symbolic link and leaving the file intact. I have read that linux uses unlink for rm--but I don't think mac os x does. If you rm a symbolic link on a mac, the link and file are gone. If you unlink the symbolic link, the link is gone, but the original file is still there.Kirov
In Ubuntu, I tested with ~/c/a/file, /c/b/, with ln -s ~/c/a ~/c/b/. In ~/c/b/, rm a/ does not work, rm -rf a/ only empties the original a. unlink a works perfectly, and unlink a/ does nothing.Fugacious
In OS X 10.10, unlink is simply an alias for rm. Goes to show that proper rm discipline should be utilized when dealing with any part of your directory structure.Dibb
In scripting rm -f will silently delete a link whether or not it exists, whereas unlink will complain if the link is not present. There is no `-f' option equivalent for unlink, makes the script more complex.Pregnancy
unlink can delete regular files. its less featureful than rm and not specific to symlinks. rm does not delete directories without the -r flag either. So I recommend preferring rm, it has flags for verbose and interactive; as well as meaningful warning and error messages.Frozen
@Yvon With or without the trailing slash?Mouse
@JoePhillips Actually, I don't remember which case when I first tried. Maybe I used both, and neither worked. It is so weird, I tried again and it's working.Tekla
@Kirov that's not correct. rm works in macosx fine for deleting a symbolic link, without removing the file.Bloodhound
Much safer than rm!Cushing
S
21

rm should remove the symbolic link.

$ cd ~
$ mkdir bar
$ ln -s bar foo
$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar

$ rm foo
$ ls -l foo
ls: cannot access foo: No such file or directory

$ ls -l bar
total 0
Schiffman answered 16/10, 2008 at 20:24 Comment(0)
L
15

OUT OF DATE ANSWER. rm -d removed sometime between 2008 and 2023.

Assuming it actually is a symlink,

$ rm -d symlink

It should figure it out, but since it can't we enable the latent code that was intended for another case that no longer exists but happens to do the right thing here.

Leavings answered 16/10, 2008 at 22:29 Comment(2)
There is no -d argument in CentOS 6.8, Coreutil 8.4 (June 2018 release) but it exist in Xubuntu 18.04, Coreutils 8.28 (January 2018)...Waldman
What system was it tested on? What shell (if relevant)?Courtesy
A
8

If rm cannot remove a symlink, perhaps you need to look at the permissions on the directory that contains the symlink. To remove directory entries, you need write permission on the containing directory.

Archer answered 16/10, 2008 at 20:21 Comment(0)
M
5

Assuming your setup is something like: ln -s /mnt/bar ~/foo, then you should be able to do a rm foo with no problem. If you can't, make sure you are the owner of the foo and have permission to write/execute the file. Removing foo will not touch bar, unless you do it recursively.

Mcclanahan answered 16/10, 2008 at 20:23 Comment(0)
W
5

I also had the same problem. So I suggest to try unlink <absolute path>.

For example unlink ~/<USER>/<SOME OTHER DIRECTORY>/foo.

Wayland answered 3/11, 2020 at 18:59 Comment(1)
If the question contained a specific name (foo), your answer should not use something confusing like "~/<USER>/<SOME OTHER DIRECTORY>/foo". Also some explanation would be helpful.Tiemannite
D
3

I had this problem with MinGW (actually Git Bash) running on a Windows Server. None of the above suggestions seemed to work. In the end I made a copy of the directory in case then deleted the soft link in Windows Explorer then deleted the item in the Recycle Bin. It made noises like it was deleting the files but didn't. Do make a backup though!

Dedal answered 30/1, 2018 at 8:39 Comment(0)
N
0

You can use unlink <filename> in the folder where you have created your symbolic link.

Nudity answered 10/10, 2020 at 4:8 Comment(1)
Isn't this already covered?Courtesy
D
0

If rm cannot remove a link, perhaps you need to look at the permissions on the directory that contains the link. To remove directory entries, you need write permission on the containing directory.

Doe answered 10/10, 2022 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.