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.
tail [OPTION]... [FILE]...
Try it
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.
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
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
+Ncounts from the top.tail -n +1prints the whole file,tail -n +2skips one line. This is the usual way to drop a header.-fversus-F. Log rotation renames the old file and creates a new one with the same name.-fkeeps following the old, renamed file;-Fnotices and switches to the new one. For real logs use-F.- Several files with
-fprint a header whenever the source changes. Use-qto suppress it, orjournalctl -ffor system logs. - Pair with
grep.tail -f app.log | grep --line-buffered ERRORshows only errors. Without--line-buffered, grep may hold output back in a block buffer. - A pipe cannot be rewound. On a pipe,
tailhas to read everything first, sotailon endless input never prints. - Truncation is detected. If a followed file shrinks,
tailsays the file was truncated and starts from the beginning. - In scripts always pass
--pidor wraptail -fintimeout, otherwise it never finishes. - Reading the middle:
head -n 20 f | tail -n 5prints lines 16 to 20.
Related
head,less,tee,grep- Lessons: Viewing Text, Disks and Logs