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?


# this works
rm foo
# versus
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.


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 which have the object open may continue to use it.

https://linux.die.net/man/2/unlink


rm应该删除符号链接。

skrall@skrall-desktop:~$ mkdir bar
skrall@skrall-desktop:~$ ln -s bar foo
skrall@skrall-desktop:~$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar
skrall@skrall-desktop:~$ rm foo
skrall@skrall-desktop:~$ ls -l foo
ls: cannot access foo: No such file or directory
skrall@skrall-desktop:~$ ls -l bar
total 0
skrall@skrall-desktop:~$ 
链接地址: http://www.djcxy.com/p/13682.html

上一篇: git如何处理符号链接?

下一篇: 删除一个符号链接到一个目录