Advertisement
Advertisement



Question

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?

2014/02/04
1
1095
2/4/2014 3:00:56 AM

Accepted Answer

# 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.

2020/01/11
1319
1/11/2020 1:58:13 AM


rm should remove the symbolic link.

[email protected]:~$ mkdir bar
[email protected]:~$ ln -s bar foo
[email protected]:~$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar
[email protected]:~$ rm foo
[email protected]:~$ ls -l foo
ls: cannot access foo: No such file or directory
[email protected]:~$ ls -l bar
total 0
[email protected]:~$ 
2008/10/16

Use rm symlinkname but do not include a forward slash at the end (do not use: rm symlinkname/). You will then be asked if you want to remove the symlink, y to answer yes.

2014/06/29

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.

2014/06/29

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.

2008/10/16

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.

2008/10/16

Source: https://stackoverflow.com/questions/210120
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]