bash 删除包含符号链接的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2977536/
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-09-17 22:10:27  来源:igfitidea点击:

Deleting a folder that contains symlinks

linuxbashmacosshell

提问by user358829

If I rm -rfa folder that has soft links in it, will it try to follow those links and delete the corresponding folder, or will it simply unlink them?

如果我rm -rf的文件夹中有软链接,它会尝试跟随这些链接并删除相应的文件夹,还是只是取消链接?

I have a copy of my home directory with symbolic links in it, and I'm scared to rm -rfit in case it follows those links and blows up the corresponding folders...

我有一个我的主目录的副本,里面有符号链接,我很害怕rm -rf它会跟随这些链接并炸毁相应的文件夹......

回答by Noufal Ibrahim

Generally speaking, rmdoesn't "delete". It "unlinks". This means that references to a file are removed by rm. When the number of references reaches zero, the file will no longer be accessible and in time, the area of disk where it resides will be used for something else.

一般来说,rm不会“删除”。它“取消链接”。这意味着对文件的引用被删除rm。当引用数达到零时,文件将不再可访问,并且及时,它所在的磁盘区域将用于其他用途。

When you rma directory, the stuff inside the directory is unlinked. Symbolic links are (sort of like) files with the name of their targets inside them and so they're just removed. To actually figure out what they're pointing to and then unlink the target is special work and so will not be done by a generic tool.

当你rm是一个目录时,目录中的东西是未链接的。符号链接是(有点像)文件,其中包含目标名称,因此它们只是被删除。真正弄清楚它们指向什么然后取消链接目标是特殊的工作,因此不会由通用工具完成。

回答by Jordan Lewis

No. rm -rf won't follow symbolic links - it will simply remove them.

不。 rm -rf 不会跟随符号链接 - 它只会删除它们。

% mkdir a                                                             
% touch a/foo
% mkdir b                                                               
% ln -s a b/a                                                           
% rm -rf b                                                              
%   ls a                                                                  
foo

回答by Bohdan

Here is axample:

这是示例:

find a b

a
a/1
a/2
b

ll

drwxr-xr-x 2 ****** ****** 4.0K Feb  6 15:11 a
lrwxrwxrwx 1 ****** ****** 1 Feb  6 15:13 b -> a

.

.

rm -rf b

gives

find a b

a
a/1
a/2

.

.

rm -rf b/

gives error:

给出错误:

rm: cannot remove `b/': Not a directory

Conclusion:

结论:

rm does not follow symlinks

rm 不遵循符号链接