Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux tail command

Reference for the Linux tail command: print the last lines or bytes of a file, start at a given line with +N, and follow growing log files with -f and -F.

tail prints the end of its input: the last 10 lines by default. It is the standard way to look at the newest entries of a log file, and with -f it keeps running and prints new lines as they are appended, which is how most people watch a service live.

Like head, it works on files or on standard input, and it can count lines or bytes.

Terminal
tail [OPTION]... [FILE]...

Try it

Bash The last lines, and starting from a line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Following a file that grows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tail [-n [+]NUM | -c [+]NUM] [-f | -F] [-q | -v] [FILE]...
Option Description
-n NUM, --lines=NUM Print the last NUM lines. With +NUM, start at line NUM and print the rest.
-c NUM, --bytes=NUM Same, counted in bytes.
-f, --follow Keep the file open and print appended data. Follows the file descriptor.
-F Like --follow=name --retry: follow the name, and reopen it if it is rotated or recreated.
--pid=PID With -f, stop when process PID ends.
-s SECONDS With -f, sleep this long between checks (polling mode).
-q, -v Never / always print file name headers.
-NUM Old shorthand for -n NUM.

Returns: 0 on success; non-zero if a file could not be read. A followed file ends only when you press Ctrl+C or a stop condition occurs.

Usage notes

  • +N counts from the top. tail -n +1 prints the whole file, tail -n +2 skips one line. This is the usual way to drop a header.
  • -f versus -F. Log rotation renames the old file and creates a new one with the same name. -f keeps following the old, renamed file; -F notices and switches to the new one. For real logs use -F.
  • Several files with -f print a header whenever the source changes. Use -q to suppress it, or journalctl -f for system logs.
  • Pair with grep. tail -f app.log | grep --line-buffered ERROR shows only errors. Without --line-buffered, grep may hold output back in a block buffer.
  • A pipe cannot be rewound. On a pipe, tail has to read everything first, so tail on endless input never prints.
  • Truncation is detected. If a followed file shrinks, tail says the file was truncated and starts from the beginning.
  • In scripts always pass --pid or wrap tail -f in timeout, otherwise it never finishes.
  • Reading the middle: head -n 20 f | tail -n 5 prints lines 16 to 20.