> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/rm.1.md).

# rm.1

`rm` — 删除目录项

## 名称

`rm`, `unlink` — 删除目录项

## 概要

`rm [-f | -i] [-dIRrvWx] file`

`unlink [--] file`

## 描述

`rm` 实用程序尝试删除命令行上指定的非目录类型文件。如果文件的权限不允许写入，且标准输入设备是终端，则会（在标准错误输出上）提示用户确认。

可用选项如下：

**`-d`** 尝试删除目录以及其他类型的文件。

**`-f`** 尝试删除文件时不提示确认，无论文件的权限如何。如果文件不存在，不显示诊断信息也不修改退出状态以反映错误。`-f` 选项覆盖任何先前指定的 `-i` 选项。

**`-i`** 在尝试删除每个文件之前请求确认，无论文件的权限如何，也无论标准输入设备是否为终端。`-i` 选项覆盖任何先前指定的 `-f` 选项。

**`-I`** 如果正在删除超过三个文件或正在递归删除目录，则请求一次确认。此选项比 `-i` 侵入性小得多，但几乎提供相同级别的误操作保护。

**`-P`** 此标志无效。仅为与 4.4BSD Lite2 向后兼容而保留。

**`-R`** 尝试删除以每个 `file` 参数为根的文件层次结构。`-R` 选项隐含 `-d` 选项。如果指定了 `-i` 选项，则在处理每个目录的内容之前（以及在尝试删除该目录之前）会提示用户确认。如果用户未作出肯定回应，则跳过以该目录为根的文件层次结构。

**`-r`** 等价于 `-R`。

**`-v`** 删除文件时显示详细信息，在文件被删除时显示它们。

**`-W`** 尝试恢复被删除的文件。目前，此选项仅可用于恢复联合文件系统中被 whiteout 覆盖的文件（参见 undelete(2)）。

**`-x`** 删除层次结构时不跨越挂载点。

`rm` 实用程序删除符号链接，而非链接所引用的文件。

尝试删除文件 **/**、**.** 或 **..** 是错误的。

当实用程序以 `unlink` 调用时，只能提供一个参数，且该参数不能是目录。在此简单操作模式下不可提供任何选项，它对传递的参数执行 unlink(2) 操作。但是，通常的选项结束分隔符 `--` 可以选择性地位于参数之前。

## 退出状态

如果所有指定的文件或文件层次结构都已删除，或者指定了 `-f` 选项且所有现有的文件或文件层次结构都已删除，`rm` 实用程序以 0 退出。如果发生错误，`rm` 以大于 0 的值退出。

## 注释

`rm` 命令使用 getopt(3) 解析其参数，这使其能够接受 ‘`--`’ 选项，该选项会导致在此点停止处理标志选项。这允许删除以短横线（‘`-`’）开头的文件名。例如：

```sh
rm -- -filename
```

使用绝对或相对路径引用也可获得相同的行为。例如：

```sh
rm /home/user/-filename
```

```sh
rm ./-filename
```

## 实例

递归删除 `foobar` 目录层次结构中包含的所有文件：

```sh
$ rm -rf foobar
```

以下任一命令将删除文件 `-f`：

```sh
$ rm -- -f
$ rm ./-f
$ unlink -f
```

## 兼容性

`rm` 实用程序与历史实现的不同之处在于，`-f` 选项仅屏蔽删除不存在文件的尝试，而不是屏蔽各种错误。`-v` 选项是非标准的，不建议在脚本中使用。

此外，历史 BSD 实现在标准输出上提示，而非标准错误输出。

`-P` 选项自 FreeBSD 13 起无效，未来可能被移除。

## 参见

chflags(1), [rmdir(1)](/man/man1/rmdir.1.md), undelete(2), unlink(2), fts(3), getopt(3), symlink(7)

## 标准

`rm` 命令遵循 IEEE Std 1003.1-2013 ("POSIX.1")。

简化的 `unlink` 命令遵循 Version 2 of the Single UNIX Specification (SUSv2)。

## 历史

`rm` 命令首次出现于 Version 1 AT\&T UNIX。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/rm.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
