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 read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
tail file.txtLast 10 linestail -n 20 fileLast 20 linestail -n +5 fileFrom line 5 onwardFollow
tail -f fileFollow file updatestail -F fileFollow with retrytail -f f1 f2Follow multipleOptions
-n NUMShow NUM lines-c NUMShow NUM bytes-qQuiet (no headers)Common
tail -f log | grep errFilter live logstail -1 fileLast line onlytail -f --pid=PIDFollow until exitDownloadable Image Preview
Failed to generate preview
Basic Usage
By default, tail displays the last 10 lines of a file.
bash
tail filename.txtCommon Options
tail Options
| -n N | Display last N lines |
| -c N | Display last N bytes |
| -f | Follow file updates in real-time |
| -F | Follow and retry if file is recreated |
| -q | Quiet mode (no headers) |
| --pid=PID | Stop 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.txtFollowing 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.logTip
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.txtPractical Examples
Monitor application logs
bash
tail -f /var/log/nginx/access.logFilter 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.logWatch multiple logs with labels
bash
tail -f access.log error.logGet 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 10Skip header line in CSV
bash
tail -n +2 data.csvInfo
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.logSummary
tail is essential for log monitoring. Key takeaways:
- Use
tail -n Nfor last N lines - Use
tail -fto follow file updates - Use
tail -Ffor rotated logs - Use
tail -n +Nto skip first N-1 lines - Combine with
grepto filter real-time logs