tail
Print the last lines of a file, and follow it as it grows
By CMD Script Team · 3 min read · Last updated
tail [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-n NUM | Print the last NUM lines instead of the default 10 |
-f | Follow the file, printing new lines as they're appended (classic for watching logs) |
-n +NUM | Print starting from line NUM to the end of the file |
-c NUM | Print the last NUM bytes instead of lines |
-F | Like -f, but also re-opens the file if it's rotated or replaced |
--pid=PID | Used with -f, stop following once the given process PID exits |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
tail prints the end of one or more files — by default the last 10 lines. Its most
famous use is tail -f, which keeps the file open and prints new lines as they're
written, making it the standard way to watch a log file in real time.
Beginner examples
tail file.txt— print the last 10 linestail -n 20 file.txt— print the last 20 linestail -f app.log— follow the file live, printing new lines as they arrivetail -n +1 file.txt— print the entire file starting from line 1 (equivalent tocat)
tail -n 30 /var/log/syslog
Advanced examples
- Follow a log that might get rotated by
logrotate, reopening automatically:tail -F app.log - Watch multiple logs at once, with headers identifying each:
tail -f access.log error.log - Stop following once a specific process exits:
tail --pid=$(pgrep myapp) -f app.log - Print starting from an arbitrary line to the end:
tail -n +250 huge_file.txt - Combine with
grepto watch for specific events live:tail -f app.log | grep --line-buffered ERROR
tail -f app.log | grep --line-buffered "ERROR"
Common mistakes
- Using
tail -fon a log file managed bylogrotateand wondering why updates stop after rotation —-Fhandles that case,-fdoesn't reliably. - Forgetting the
+intail -n +5, which means "starting at line 5";tail -n 5means "the last 5 lines" instead — very different behavior. - Piping
tail -finto another command without--line-buffered(forgrep) or equivalent, which can cause output to appear in large delayed chunks instead of live. - Not stopping a
tail -fsession left running in an unattended terminal or script, wasting a process indefinitely.
Tips
Ctrl+Cstopstail -f; it doesn't stop on its own since it's designed to run indefinitely.- Use
tail -f file | grep --line-buffered patternwhen you want to watch for specific events in a live log without losing real-time output. - For quickly checking "did anything log after I did X,"
tail -n 100 file.logright after the action is often faster than starting a live follow session.
Best practices
- Prefer
tail -Fover-ffor long-running log-watching in production, since it survives log rotation without needing to be restarted. - Combine
tail -fwithgrep --line-buffered(orawk's equivalent) rather than plaingrepwhen filtering live streams, so output isn't buffered and delayed. - Use
--pid=PIDwhen scripting a wait-for-log-line pattern, so thetailprocess exits cleanly instead of running forever if the target process dies.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Watching an application's log output live while reproducing a bug or deploying a
change:
tail -f app.log. - Debugging a service startup by following its log until a "ready" message appears.
- Quickly checking the most recent entries in a crash log after an incident:
tail -n 200 /var/log/syslog.
Common interview questions
- How do you watch a log file update in real time?
tail -f logfile.log, which keeps the file open and prints new lines as they're appended. - What's the difference between tail -f and tail -F?
-ffollows the open file descriptor and can miss updates after log rotation;-Ffollows the filename and reopens the file if it's rotated or recreated. - How would you print a file starting at line 100 to the end?
tail -n +100 file.txt— note the leading+, which means "from this line onward" rather than "the last N lines."
Frequently Asked Questions
How do I watch a log file update live?
Use tail -f logfile.log. It keeps running and prints new lines as they're appended; press Ctrl+C to stop.
What's the difference between tail -f and tail -F?
-f follows the file descriptor, so if the file is rotated or deleted and recreated (common with logrotate), it can stop updating. -F follows the filename instead, reopening the new file automatically after rotation.
How do I print a file starting from a specific line?
Use tail -n +5 file.txt to print from line 5 to the end. Note the plus sign — without it, -n 5 would mean the last 5 lines instead.
How do I stop tail -f from a script once a background process finishes?
Use tail --pid=$PID -f logfile, which automatically stops following once the process with that PID exits — handy for waiting on a service's startup log.
Cheat sheet
Download a quick-reference cheat sheet for tail.