Linux 删除指向目录的符号链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/210120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:37:06  来源:igfitidea点击:

Remove a symlink to a directory

linuxfilesymlink

提问by Matthew Scouten

I have a symlink to an important directory. I want to get rid of that symlink, while keeping the directory behind it.

我有一个指向重要目录的符号链接。我想摆脱那个符号链接,同时将目录保留在它后面。

I tried rmand get back rm: cannot remove 'foo'.
I tried rmdirand got back rmdir: failed to remove 'foo': Directory not empty
I then progressed through rm -f, rm -rfand sudo rm -rf

我试着rm回来了rm: cannot remove 'foo'
我尝试rmdir并回来了rmdir: failed to remove 'foo': Directory not empty
,然后我继续前进rm -frm -rf并且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?

有没有办法摆脱符号链接而不用洗澡水扔掉婴儿?

采纳答案by Matthew Scharley

# 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 rmand rmdirexists because of differences in the way the C library treats each.

基本上,您需要告诉它删除文件,而不是删除目录。我相信之间的差异rmrmdir存在的,因为在C库将每一个的方式不同。

At any rate, the first should work, while the second should complain about foo being a directory.

无论如何,第一个应该可以工作,而第二个应该抱怨 foo 是一个目录。

If it doesn't work as above, then check your permissions. You need write permission to the containing directory to remove files.

如果它不能按上述方式工作,请检查您的权限。您需要对包含目录的写权限才能删除文件。

回答by Greg Hewgill

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.

如果 rm 无法删除符号链接,也许您需要查看包含该符号链接的目录的权限。要删除目录条目,您需要对包含目录的写权限。

回答by Joe Phillips

use the "unlink" command and make sure notto 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.

unlink() 从文件系统中删除一个名称。如果该名称是文件的最后一个链接,并且没有进程打开该文件,则该文件将被删除,并且它所使用的空间可供重用。如果名称是文件的最后一个链接,但任何进程仍然打开该文件,则该文件将一直存在,直到最后一个引用它的文件描述符关闭。

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.

如果名称引用符号链接,则链接将被删除。

如果名称引用了套接字、FIFO 或设备,则删除它的名称,但打开对象的进程可以继续使用它。

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

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

回答by TJ L

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

假设您的设置类似于:ln -s /mnt/bar ~/foo,那么您应该可以rm foo毫无问题地进行操作。如果不能,请确保您是该文件的所有者foo并有权写入/执行该文件。删除foo不会触及bar,除非您递归地进行。

回答by Steve K

rm should remove the symbolic link.

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:~$ 

回答by Joshua

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.

它应该弄清楚,但由于我们无法启用用于另一个不再存在但碰巧在这里做正确事情的潜在代码。

回答by DeeEss09

Use rm symlinknamebut 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, yto answer yes.

使用rm symlinkname但不要在末尾包含正斜杠(不要使用:)rm symlinkname/。然后将询问您是否要删除符号链接,y回答是。

回答by Yuri

On CentOS, just run rm linknameand it will ask to "remove symbolic link?". Type Yand Enter, the link will be gone and the directory be safe.

在 CentOS 上,只要运行rm linkname它就会询问“删除符号链接?”。键入YEnter,链接将消失并且目录是安全的。

回答by Keith Whittingham

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 a 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!

我在 Windows Server 上运行 MinGW(实际上是 Git Bash)时遇到了这个问题。上述建议似乎都不起作用。最后复制该目录以防万一然后删除Windows资源管理器中的软链接,然后删除回收站中的项目。它发出的声音就像删除文件一样,但没有。不过一定要备份!