Last updated:

Quick answer: tail is a Linux and Unix command that prints the end of a file. By default it shows the last 10 lines. Use tail -n 50 file for the last 50 lines, tail -n +50 file to start at line 50, and tail -f file to keep watching a log as new lines are written (press Ctrl+C to stop). For logs that get rotated, use tail -F.

Command What it does
tail file Last 10 lines
tail -n 20 file (or tail -20 file) Last 20 lines
tail -n +20 file Everything from line 20 to the end
tail -c 100 file Last 100 bytes
tail -f file Follow the file and print new lines as they arrive
tail -F file Follow by name and reopen the file if it is rotated or recreated
tail -f --pid=1234 file Stop following when process 1234 exits
tail -q -n 5 a.log b.log Several files without the ==> name <== headers

Every example below was run on Ubuntu 24.04 with tail (GNU coreutils) 9.4 on 11 November 2026, and the output blocks are copied from that terminal. GNU tail is the version on Ubuntu, Debian, Linux Mint, Fedora and most other Linux distributions; macOS and BSD ship a different tail that supports the common options (-n, -f, -F, -c) but not all GNU extras such as --pid.

Show the last lines of a file

Decorative title graphic reading LINUX is not UNIX

The test file used below contains 15 lines, line 1 to line 15 (created with seq -f "line %g" 1 15 > demo.txt). Without options, tail prints the last 10:

$ tail demo.txt
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15

To choose the number of lines, use -n. The short form -3 does the same thing:

$ tail -n 3 demo.txt
line 13
line 14
line 15
Advertisement

Start from a line number with tail -n +N

A plus sign changes the meaning: -n +13 means “start at line 13”, not “the last 13 lines”. This is handy for skipping a header row, for example tail -n +2 data.csv prints a CSV file without its first line.

$ tail -n +13 demo.txt
line 13
line 14
line 15

To print a range from the middle of a file, combine head and tail (or use sed). Both commands below print lines 6 to 8:

$ head -n 8 demo.txt | tail -n 3
line 6
line 7
line 8
$ sed -n 6,8p demo.txt
line 6
line 7
line 8

Decorative title graphic reading LINUX Heads and Tails

Show the last bytes with tail -c

-c counts bytes instead of lines. The last line of the test file, line 15 plus its newline character, is exactly 8 bytes:

$ tail -c 8 demo.txt
line 15

Several files at once

With more than one file, tail prints a header before each file. -q (quiet) removes the headers:

$ tail -n 2 demo.txt other.txt
==> demo.txt <==
line 14
line 15

==> other.txt <==
a
b
$ tail -q -n 1 demo.txt other.txt
line 15
b
Advertisement

Watch a log in real time with tail -f

-f (follow) prints the end of the file and then keeps running, printing each new line as a program writes it. This is the usual way to watch a web server or application log while you reproduce a problem, for example sudo tail -f /var/log/nginx/error.log while testing an nginx reverse proxy. In our test, two lines were appended to app.log by another process while tail -f was running:

$ tail -f app.log
boot ok
GET /index.html 200
GET /missing 404
^C

Press Ctrl+C to stop. To show only interesting lines, pipe the output into grep. The --line-buffered option makes grep print each match immediately instead of waiting to fill a buffer:

$ tail -f web.log | grep --line-buffered -E " (404|500)$"
GET /x 404
GET /y 500

Decorative graphic of the Linux penguin mascot

tail -f vs tail -F: rotated logs

Tools such as logrotate rename a log (for example app.log to app.log.1) and start a new, empty file. tail -f keeps following the old file it opened, so it goes quiet after rotation. tail -F follows the file name and reopens it, which is what you want for logs. We tested both by renaming the file and creating a new one while tail was running.

With -f, the line written to the new file never appeared:

Stuck at this step? Ask us, it is free and include the exact error.

$ tail -f rot2.log
old entry

With -F, tail noticed the change and switched to the new file:

$ tail -F rot.log
old entry
tail: 'rot.log' has become inaccessible: No such file or directory
tail: 'rot.log' has appeared;  following new file
new file entry
second entry

-F is the same as --follow=name --retry, so it also waits for a file that does not exist yet. On GNU tail, -s N changes how many seconds tail sleeps between checks when it has to poll; it is optional, and you do not need it together with -f.

Stop following when a process ends (–pid)

If you start a long job that writes a log, --pid makes tail exit on its own when that process finishes. In our test the process ran for 3 seconds, and tail exited with status 0 after 3.0 seconds:

./long-job.sh > job.log 2>&1 &
tail -f --pid=$! job.log

$! is the process ID of the last command started in the background.

Decorative title graphic reading Tail LINUX Specifics and Examples

Reading system logs: permissions and journalctl

Some files under /var/log are readable only by root or the adm group. Reading one as a normal user fails like this:

$ tail -n 2 /root/tailt/root-only.log
tail: cannot open '/root/tailt/root-only.log' for reading: Permission denied

Use sudo tail ... for those files. World-readable logs such as the package log work without sudo; on our test system the last three lines of /var/log/dpkg.log were:

$ tail -n 3 /var/log/dpkg.log
2026-11-11 06:28:47 status unpacked golang-1.26-go:amd64 1.26.8-1~ubuntu0.24.04.2
2026-11-11 06:28:47 status half-configured golang-1.26-go:amd64 1.26.8-1~ubuntu0.24.04.2
2026-11-11 06:28:47 status installed golang-1.26-go:amd64 1.26.8-1~ubuntu0.24.04.2

Many services on modern distributions write to the systemd journal instead of a text file, so there may be nothing to tail. The journal has its own equivalents: journalctl -n 50 for the last 50 entries and journalctl -u nginx -f to follow one service. Our guide to creating a systemd service shows how to read a service’s log this way.

Common mistakes and errors

Problem Why it happens Fix
tail -f stops showing new lines after midnight or a restart The log was rotated; -f follows the old file Use tail -F
tail -n +5 prints far more than 5 lines The plus sign means “start at line 5” Use tail -n 5 for the last five lines
Permission denied The log belongs to root or the adm group Run with sudo
tail -f file | grep x shows nothing for a while grep buffers its output when writing to a pipe or file Add --line-buffered to grep
Empty or missing log file The service logs to the systemd journal Use journalctl -u service -f

Decorative title graphic reading Tail LINUX Wrap-up

tail vs head vs less

head is the mirror of tail: it prints the first 10 lines (head -n 20 file for 20). less +F file follows a file like tail -f, but you can press Ctrl+C to stop following, scroll back and search, then press F to resume. Use tail for quick checks and scripts, and less when you need to look around in a large log. The full option list is in man tail or tail --help.

FAQ

What is tail in Linux?

It is a standard command-line program (part of GNU coreutils on Linux) that outputs the last part of files, 10 lines by default, and can keep printing new lines with -f.

What does tail -10 mean?

It is the short form of tail -n 10: print the last 10 lines, which is also the default.

How do I exit tail -f?

Press Ctrl+C. The file is not changed; tail only reads it.

Advertisement