tail – output the last part of files

tail command
tail command

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

OptionDescription
-n NShow last N lines
-c NShow last N bytes
-fFollow file (watch for new lines)
-FFollow and retry if file is recreated
-qQuiet, don’t print headers
--pid=PIDStop 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 -f in separate terminals for different logs
  • Alternative: less +F provides follow mode with search capability
  • Multitail: For advanced multi-file following with colors

See Also

  • head — Show first lines of file
  • cat — Show entire file
  • less — Interactive viewer (supports +F follow)
  • watch — Run command repeatedly

Tutorials