tail – output the last part of files

tail displays the last lines of a file. It’s especially useful for monitoring log files in real-time.
Synopsis
tail [OPTIONS] [FILE...]
Common Options
| Option | Description |
|---|---|
-n N | Show last N lines |
-c N | Show last N bytes |
-f | Follow file (watch for new lines) |
-F | Follow and retry if file is recreated |
-q | Quiet, don’t print headers |
--pid=PID | Stop following when PID dies |
Examples
Show last 10 lines (default)
$ tail /var/log/syslog
Show last N lines
$ tail -n 20 /var/log/auth.log
$ tail -20 /var/log/auth.log # Shorthand
Show last N bytes
$ tail -c 500 logfile.txt
Follow a log file (live updates)
$ tail -f /var/log/syslog
# Press Ctrl+C to stop
Follow with retry (for rotating logs)
$ tail -F /var/log/nginx/access.log
Follow multiple files
$ tail -f /var/log/syslog /var/log/auth.log
Start from line N
$ tail -n +100 file.txt # Start from line 100
Combine with grep
$ tail -f /var/log/syslog | grep --line-buffered "error"
Follow until process ends
$ tail -f logfile.log --pid=$$
# Stops when current shell exits
Common Patterns
Watch for errors in real-time
$ tail -f /var/log/syslog | grep -i error
Last N lines with line numbers
$ tail -n 20 file.txt | nl
Get lines from middle of file
$ head -n 200 file.txt | tail -n 50 # Lines 151-200
Monitor Apache/Nginx access
$ tail -f /var/log/nginx/access.log | awk '{print $1, $7}'
Follow and highlight
$ tail -f logfile.log | grep --color=always -E 'error|warning|$'
Tips
- Use -F for log rotation: Handles logrotate gracefully
- Add –line-buffered to grep: When piping tail -f output
- Multiple windows: Run
tail -fin separate terminals for different logs - Alternative:
less +Fprovides follow mode with search capability - Multitail: For advanced multi-file following with colors
See Also
Related Commands
- head — Show first lines of file
- cat — Show entire file
- less — Interactive viewer (supports +F follow)
- watch — Run command repeatedly






