rm -r 和 rm -f 有什么区别?

Cer*_*ian 4 terminal rm

来自手册:

  • -f,--强制

    忽略不存在的文件,从不提示

  • -r, -R, --递归

    递归删除目录内容

尽管此选项描述不同,但当尝试删除空文件夹(本例中没有 rmdir)时,它会给出相同的结果。

-f不会打印错误或与 相比的任何内容-r,这是唯一的区别还是存在特定类型的情况,当一个选项比另一个选项更好时,或者其中一个选项根本不起作用而另一个选项会起作用的情况?

Cas*_*Cas 7

CentOS 的手册页是这样说的:

-f, --force
    ignore nonexistent files, never prompt

-r, -R, --recursive
    remove directories and their contents recursively
Run Code Online (Sandbox Code Playgroud)

根据我收集的信息(感谢下面的一些评论),以下内容对于-r-f标志是正确的:

-r

  • 递归删除目录内容,包括隐藏文件和子目录
  • 根据您的配置,它可能会请求许可(例如,在使用标志时--interactive)。有些发行版默认这样做。
  • 可以用来删除目录,如果你想这样做,只需给它目录的路径(例如:/path/to/directory

-F

  • 不会递归删除目录的内容,仅删除与给定路径直接匹配的文件(例如example/file1example/*)。
  • 从不删除子目录
  • 从不请求许可,基本上是yes to allWindows 中的

下面是一些示例,它们都以以下结构开头:

example/
  file1
  file2
  file3
  .file
  dir/
    file1
    file2
    file3
    .file
Run Code Online (Sandbox Code Playgroud)

我默认为这些示例启用了详细程度和交互模式。有些发行版这样做,而另一些则不这样做。

例子

$ rm example
rm: cannot remove `example': Is a directory
Run Code Online (Sandbox Code Playgroud)

如您所见,rm默认情况下不删除目录。

rm 示例 -f

$ rm example -f
rm: cannot remove `example': Is a directory
Run Code Online (Sandbox Code Playgroud)

使用该-f标志仍然不允许它删除目录。

rm 示例 -r

$ rm example -r
rm: descend into directory `example'? yes
rm: remove regular empty file `example/file3'? yes
  removed `example/file3'
rm: remove regular empty file `example/file2'? yes
  removed `example/file2'
rm: descend into directory `example/dir'? yes
rm: remove regular empty file `example/dir/.file'? yes
  removed `example/dir/.file'
rm: remove regular empty file `example/dir/file3'? yes
  removed `example/dir/file3'
rm: remove regular empty file `example/dir/file2'? yes
  removed `example/dir/file2'
rm: remove regular empty file `example/dir/file1'? yes
  removed `example/dir/file1'
rm: remove directory `example/dir'? yes
  removed directory: `example/dir'
rm: remove regular empty file `example/file1'? yes
  removed `example/file1'
rm: remove directory `example'? yes
  removed directory: `example'
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,您需要获得每个文件和目录的权限,隐藏文件也会被删除。

rm 示例/* -f

$ rm example/* -f
rm: cannot remove `example/dir': Is a directory
removed `example/file1'
removed `example/file2'
removed `example/file3'
Run Code Online (Sandbox Code Playgroud)

在这里,不会询问您的权限,不会删除目录,也不会删除隐藏文件。

rm 示例/* -r

$ rm example/* -r
rm: descend into directory `example/dir'? yes
rm: remove regular empty file `example/dir/.file'? yes
  removed `example/dir/.file'
rm: remove regular empty file `example/dir/file3'? yes
  removed `example/dir/file3'
rm: remove regular empty file `example/dir/file2'? yes
  removed `example/dir/file2'
rm: remove regular empty file `example/dir/file1'? yes
  removed `example/dir/file1'
rm: remove directory `example/dir'? yes
  removed directory: `example/dir'
rm: remove regular empty file `example/.file'? yes
  removed `example/file'
rm: remove regular empty file `example/file1'? yes
  removed `example/file1'
rm: remove regular empty file `example/file2'? yes
  removed `example/file2'
rm: remove regular empty file `example/file3'? yes
  removed `example/file3'
Run Code Online (Sandbox Code Playgroud)

此处,示例目录的内容(不是目录本身)被删除,包括隐藏文件。