Terminal GuideTerminal Guide

tail Command Guide

The tail command displays the last part of files. It's especially useful for monitoring log files in real-time.

5 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

tail file.txtLast 10 lines
tail -n 20 fileLast 20 lines
tail -n +5 fileFrom line 5 onward

Follow

tail -f fileFollow file updates
tail -F fileFollow with retry
tail -f f1 f2Follow multiple

Options

-n NUMShow NUM lines
-c NUMShow NUM bytes
-qQuiet (no headers)

Common

tail -f log | grep errFilter live logs
tail -1 fileLast line only
tail -f --pid=PIDFollow until exit

Downloadable Image Preview

Failed to generate preview

Basic Usage

By default, tail displays the last 10 lines of a file.

bash
tail filename.txt

Common Options

tail Options

-n NDisplay last N lines
-c NDisplay last N bytes
-fFollow file updates in real-time
-FFollow and retry if file is recreated
-qQuiet mode (no headers)
--pid=PIDStop following when PID dies

Specifying Number of Lines

bash
# Display last 20 lines
tail -n 20 file.txt

# Short form
tail -20 file.txt

# Start from line 50 (skip first 49 lines)
tail -n +50 file.txt

Following Files in Real-Time

The -f option is tail's most powerful feature for monitoring logs.

bash
# Follow log file updates
tail -f /var/log/syslog

# Follow with more context
tail -n 50 -f /var/log/syslog

# Follow multiple files
tail -f file1.log file2.log

-f vs -F

bash
# -f: Follows file descriptor (fails if file is rotated)
tail -f app.log

# -F: Follows filename (handles log rotation)
tail -F app.log
Tip
Use -F for log files that get rotated (like Apache or nginx logs) to continue following after rotation.

Display Bytes

bash
# Last 100 bytes
tail -c 100 file.txt

# Last 1KB
tail -c 1K file.txt

Practical Examples

Monitor application logs

bash
tail -f /var/log/nginx/access.log

Filter while following

bash
# Watch for errors only
tail -f app.log | grep --line-buffered "ERROR"

# Highlight patterns
tail -f app.log | grep --color=always -E "ERROR|WARNING|$"

Follow until process exits

bash
# Follow log until server process (PID 1234) exits
tail -f --pid=1234 server.log

Watch multiple logs with labels

bash
tail -f access.log error.log

Get last N lines from command output

bash
# Last 5 most recently modified files
ls -lt | tail -n 5

# Last 10 history entries
history | tail -n 10

Skip header line in CSV

bash
tail -n +2 data.csv
Info
Press Ctrl+C to stop following a file with tail -f.

Advanced: Using with multitail

For more advanced log monitoring, consider multitail.

bash
# Install on Debian/Ubuntu
sudo apt install multitail

# Monitor multiple logs in split view
multitail /var/log/syslog /var/log/auth.log

Summary

tail is essential for log monitoring. Key takeaways:

  • Use tail -n N for last N lines
  • Use tail -f to follow file updates
  • Use tail -F for rotated logs
  • Use tail -n +N to skip first N-1 lines
  • Combine with grep to filter real-time logs

Related Articles