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