Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

tail Command Guide for Viewing the End of a File and Following It Live

tail prints the last lines of a file — and with -f, it keeps watching and shows new lines the moment they’re written, making it the standard tool for following logs.


What is tail, and Why Use It?

tail shows the end of a file: the last 10 lines by default, or any number you ask for. Its killer feature is -f (“follow”), which keeps the file open and prints every new line as it arrives.

That’s why tail -f is the command for watching logs: server activity, build output, application errors — all live, in your terminal.

Why Learn tail?

  • Logs grow at the bottom. Errors, status changes, and the most recent events are always at the end — tail goes straight there.
  • tail -f turns your terminal into a live monitor without any extra software.
  • It’s the natural partner of head: together they slice a file from both ends.
  • Scripts and pipelines lean on it for “last N events” checks and for feeding streams into other tools.

When something is happening right now, tail -f is how you see it happen.


1. Print the Last 10 Lines

tail filename.txt

Example:

tail /var/log/syslog
  • The default is the final 10 lines — usually enough to see the current state or the most recent error.

2. Choose How Many Lines

tail -n 50 filename.txt

Example:

tail -n 100 access.log
  • -n 50 prints the last 50 lines. tail -50 file.txt also works, but -n is the explicit, portable form.

3. Follow a File Live

tail -f filename.txt

Example:

tail -f /var/log/syslog
  • New lines appear as they’re written. Press Ctrl+C to stop.
  • tail -f works by watching the file, so it survives log rotation on most systems when you use --follow=name --retry (see below).

4. Follow Through Log Rotation

tail -F filename.txt
  • Capital -F is equivalent to --follow=name --retry: if the file is rotated, renamed, or recreated (common with nightly log rotation), tail re-opens the new file instead of silently following a deleted inode. For long-running log watching, prefer -F.

5. Count Lines from the End by Bytes

tail -c 200 filename.txt

Example:

tail -c 1000 access.log
  • -c 200 prints the last 200 bytes — handy when lines are very long or you want a fixed-size window regardless of line count.

6. Follow Several Files at Once

tail -f file1.log file2.log

Example:

tail -f app.log error.log
  • Each file gets a header when it’s first shown, and updates interleave as they arrive. Useful for watching an app and its error log side by side.

7. Sample the Tail of a Stream

yes | tail -n 3

Example:

du -sh /* | tail -n 5
  • Piping into tail gives you the final lines of any command’s output — the “last few entries” of a directory listing, a process list, or a log dump.

Common Pitfalls

  • tail -f vs tail -F after rotation. Plain -f keeps following the old (now-unlinked) file and shows nothing new. Use -F when logs rotate.
  • tail -f on a huge file still reads the whole file first on some implementations to find the end. For instant “last N lines”, that’s fine; for GB-sized files on slow disks, expect a brief pause.
  • Binary files print garbage and can leave your terminal in a weird state. Run reset if the prompt looks wrong afterwards.
  • tail and head disagree with wc -l on files without a trailing newline. The last incomplete line may not be counted the same way — a classic off-by-one in scripts.

CommandDescription
headThe first lines of a file
lessScroll a whole file (+F also follows)
catDump the whole file at once
grepFilter lines from a followed stream
journalctlThe systemd-native log viewer

tail Cheatsheet

CommandWhat It Does
tail fileLast 10 lines
tail -n 50 fileLast 50 lines
tail -f fileFollow new lines live
tail -F fileFollow through log rotation
tail -c 200 fileLast 200 bytes
tail -f a.log b.logFollow several files at once
command | tail -n 5Last 5 lines of any output