> 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/tail.1.md).

# tail.1

`tail` — 显示文件的最后部分

## 名称

`tail`

## 概要

`tail [-F | -f | -r] [-qv] [-b number | -c number | -n number] [file]`

## 描述

`tail` 实用程序显示 `file` 的内容，或默认情况下将其标准输入的内容显示到标准输出。

显示从输入中的字节、行或 512 字节块位置开始。带前导加号（`+`）的数字相对于输入开头，例如“`-c +2`”从输入的第二个字节开始显示。带前导减号（`-`）或无显式符号的数字相对于输入末尾，例如“`-n 2`”显示输入的最后两行。默认起始位置是“`-n 10`”，即输入的最后 10 行。

选项如下：

**`-b`** `number`、`--blocks`=`number` 位置为 `number` 个 512 字节块。

**`-c`** `number`、`--bytes`=`number` 位置为 `number` 个字节。

**`-f`** `-f` 选项使 `tail` 在到达文件末尾时不停止，而是等待附加数据被追加到输入。如果标准输入是管道，`-f` 选项会被忽略，但 FIFO 不会。

**`-F`** `-F` 选项隐含 `-f` 选项，但 `tail` 还会检查被跟踪的文件是否已被重命名或轮转。当 `tail` 检测到正在读取的文件名具有新的 inode 号时，文件会被关闭并重新打开。如果被跟踪的文件（尚）不存在或被删除，tail 会持续查找，并在文件创建时从头显示。如果从标准输入而非文件读取，`-F` 选项与 `-f` 选项相同。

**`-n`** `number`、`--lines`=`number` 位置为 `number` 行。

**`-q`**、`--quiet`、`--silent` 当检查多个文件时，禁止打印文件头。

**`-r`** `-r` 选项使输入按行逆序显示。此外，此选项改变了 `-b`、`-c` 和 `-n` 选项的含义。当指定 `-r` 选项时，这些选项指定要显示的字节、行或 512 字节块的数量，而不是从输入开头或末尾开始显示的字节、行或块。`-r` 选项的默认行为是显示全部输入。

**`-v`**、`--verbose` 在每个文件前添加文件头。

如果指定了多个文件，或使用了 `-v` 选项，每个文件前都会有一个由字符串 “==> `XXX` <==” 组成的文件头，其中 `XXX` 是文件名。`-q` 选项在所有情况下都禁止打印文件头。

所有 `number` 参数也可使用 expand\_number(3) 所支持的大小后缀来指定。

## 退出状态

`tail` 实用程序成功时退出值为 0，发生错误时大于 0。

## 实例

显示文件 `foo` 的最后 500 行：

```sh
$ tail -n 500 foo
```

保持 **/var/log/messages** 打开，将任何追加到文件的内容显示到标准输出：

```sh
$ tail -F /var/log/messages
```

从开头读取 **/var/log/messages**，然后像往常一样跟踪文件：

```sh
$ tail -F -n +1 /var/log/messages
```

## 参见

[cat(1)](/man/man1/cat.1.md), [head(1)](/man/man1/head.1.md), [sed(1)](/man/man1/sed.1.md), expand\_number(3)

## 标准

`tail` 实用程序预期是 ISO/IEC 9945-2:1993（“POSIX.2”）规范的超集。特别是，`-F`、`-b` 和 `-r` 选项是该标准的扩展。

此实现支持 `tail` 的历史命令行语法。在完成命令行语法转换后，此实现与历史版本的 `tail` 的唯一区别是，`-b`、`-c` 和 `-n` 选项会修改 `-r` 选项，即 “`-r -c 4`” 显示输入最后一行的最后 4 个字符，而历史 tail（使用历史语法 “`-4cr`”）会忽略 `-c` 选项并显示输入的最后 4 行。

## 历史

`tail` 命令首次出现于 PWB 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/tail.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.
