首頁·終端機命令

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