首页·终端命令

tail

文件与目录

用法

tail -f [file]

打印文件末尾,并用 -f 持续跟着看;日志轮转之后还想跟上,必须用 -F。

常用选项

选项含义
-nPrint the last N lines
-n +NStart at line N instead of counting from the end
-fKeep printing as the file grows
-FFollow by name and reopen the file after log rotation
-cPrint the last N bytes
-rBSD: print the lines in reverse order (GNU uses tac)

示例

tail -f app.log

Watches a log live; Ctrl-C stops.

tail -n +2 data.csv

Skips the header line.

tail -F /var/log/nginx/access.log

Keeps following after the log is rotated.

移动、删除和查找。有些命令不会再问一遍,所以顺手加 -i 是在救自己。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. tail 是做什么的?

打印文件末尾,并用 -f 持续跟着看;日志轮转之后还想跟上,必须用 -F。

Q. 怎么写?

tail -f [file] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man tail。这条命令属于文件与目录。

相关命令

man 手册: man tail