Bash:“rm -d”和“rm -R”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11976861/
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
Bash: What's the difference between "rm -d" and "rm -R"?
提问by caleb531
Questions
问题
- What is the difference between the
rm -dandrm -Rcommands in Bash? - Which one should I use?
- Bash 中的
rm -d和rm -R命令之间有什么区别? - 我应该使用哪一种?
Details
细节
According to the man page for the rmcommand:
根据rm命令的手册页:
rm -dattempts to remove directories as well as other types of files.rm -Rattempts to remove the file hierarchy rooted in each file argument. The -R option implies the -d option.
rm -d尝试删除目录以及其他类型的文件。rm -R尝试删除以每个文件参数为根的文件层次结构。-R 选项意味着 -d 选项。
Now, I am aware of that last statement (-Rimplies -d), which may seem to answer my question. However, I still wonder why both command flags exist in the first place, if they are supposedly identical in what they do.
现在,我知道最后一个陈述(-R暗示-d),它似乎可以回答我的问题。但是,我仍然想知道为什么这两个命令标志首先存在,如果它们的作用应该相同。
Furthermore, because I am still in the process of learning Bash, I think it's good to know which option is the preferred choice among Bash programmers (conventionally), and why.
此外,因为我仍在学习 Bash 的过程中,我认为最好知道哪个选项是 Bash 程序员的首选(传统上),以及为什么。
回答by chepner
Ordinarily, rmwill not remove a directory, even if it is empty. rm -djust makes rmact like rmdir. It still refuses to remove the directory if it isn't empty, but will do so if it is empty.
通常,rm不会删除目录,即使它是空的。rm -d只是让rm行为像rmdir。如果目录不是空的,它仍然拒绝删除目录,但如果它是空的,它会这样做。
rm -Ris the full recursive delete, removing the directory and all its contents.
rm -R是完全递归删除,删除目录及其所有内容。
I've never used -d, as I didn't know it existed and always just use rmdir. I'd use rmdir/rm -dif you only want to remove the directory if it is, in fact, empty. Save rm -Rfor when you are fully aware that you are trying to remove a directory and all its contents.
我从未使用过-d,因为我不知道它存在并且总是使用rmdir. 如果您只想删除目录,如果它实际上是空的,我会使用rmdir/ rm -d。保存rm -R时,您知道您试图删除目录及其所有内容的。
回答by Grisha Levit
The -doption is particular to the BSD implementation of rm, which is the one you are likely finding on your Mac. It is not present in the GNU implementation you will find on Linux systems.
该-d选项特定于 的 BSD 实现rm,您可能会在 Mac 上找到它。它不存在于您将在 Linux 系统上找到的 GNU 实现中。
If you are looking for the preferred choice, it would be to use -r(lowercase) to remove whole trees and rmdirfor removing single directories, which you will find to be code that is more portable.
如果您正在寻找首选选项,则可以使用-r(小写)删除整个树和rmdir删除单个目录,您会发现这些代码更易于移植。
回答by Abdur Rahman Turawa
rm will delete files, but i had issues trying to remove directory, so it need to be flagged with some option,
rm 将删除文件,但我在尝试删除目录时遇到问题,因此需要使用某些选项对其进行标记,
rm -f will force remove file without asking for confirmation
rm -f 将强制删除文件而不要求确认
rm -R will remove directory but in my case was asking for confirmation, but because i have so many files, i kept on typing y and y which was taken for eternity
rm -R 将删除目录,但在我的情况下是要求确认,但因为我有这么多文件,我一直在输入 y 和 y,这是永恒的
rm -Rf was my final solution, it forced remove the directory without asking for confirmation
rm -Rf 是我的最终解决方案,它强制删除目录而不要求确认
回答by Alan Curry
Just to be clear, you should never use rm -d. Assuming it doesn't just fail with an "Operation not permitted" error message, it will remove the directory withoutremoving the contents. On an empty directory it's the same as rmdir. On a non-empty directory it creates an inconsistency in the filesystem requiring repair by fsck or by some very clever manual hacking.
为了清楚起见,您永远不应该使用 rm -d。假设它不只是失败并显示“不允许操作”错误消息,它将删除目录而不删除内容。在空目录上,它与rmdir. 在非空目录上,它会在文件系统中造成不一致,需要通过 fsck 或一些非常聪明的手动黑客进行修复。
It's a stupid option that should never have existed. The BSD people were on some bad drugs when they added it. rm -rhad been in UNIX since at least 1973, and rmdirsince 1971.
这是一个不应该存在的愚蠢选择。BSD 的人在添加它时正在服用一些不好的药物。rm -r至少从 1973 年和rmdir1971 年开始使用 UNIX 。

