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 —
tailgoes straight there. tail -fturns 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 -fis 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 50prints the last 50 lines.tail -50 file.txtalso works, but-nis 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 -fworks 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
-Fis equivalent to--follow=name --retry: if the file is rotated, renamed, or recreated (common with nightly log rotation),tailre-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 200prints 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
tailgives 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 -fvstail -Fafter rotation. Plain-fkeeps following the old (now-unlinked) file and shows nothing new. Use-Fwhen logs rotate.tail -fon 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
resetif the prompt looks wrong afterwards. tailandheaddisagree withwc -lon files without a trailing newline. The last incomplete line may not be counted the same way — a classic off-by-one in scripts.
Related Commands
| Command | Description |
|---|---|
| head | The first lines of a file |
| less | Scroll a whole file (+F also follows) |
| cat | Dump the whole file at once |
| grep | Filter lines from a followed stream |
| journalctl | The systemd-native log viewer |
tail Cheatsheet
| Command | What It Does |
|---|---|
tail file | Last 10 lines |
tail -n 50 file | Last 50 lines |
tail -f file | Follow new lines live |
tail -F file | Follow through log rotation |
tail -c 200 file | Last 200 bytes |
tail -f a.log b.log | Follow several files at once |
command | tail -n 5 | Last 5 lines of any output |