Bash View End (tail)
Display the last lines of files
📄 What is the tail Command?
The tail command displays the last lines of a file. By default, it shows the last 10 lines, making it perfect for viewing recent log entries or checking file endings quickly.
# View last 10 lines
tail logfile.txt
Tail Features
Last Lines
View end of files
tail file.txt
Custom Count
Specify number of lines
tail -n 20 file.txt
Follow Mode
Watch file updates live
tail -f log.txt
Multiple Files
View several files at once
tail file1.txt file2.txt
🔹 Basic Usage
The tail command is a core utility for viewing the end portion of files, particularly useful for logs and large text files. Its default behavior, invoked simply as tail filename, is to output the last 10 lines to the terminal. This provides a quick, resource-efficient way to inspect the most recent entries without loading the entire file. It is indispensable for system administrators checking the latest system log entries (/var/log/syslog), developers verifying the end of application output, or anyone needing a snapshot of recent data. The command reads input from files or standard input, making it highly versatile in pipelines.
# Create a sample file
seq 1 20 > numbers.txt
# View last 10 lines
tail numbers.txt
Output:
11 12 13 14 15 16 17 18 19 20
🔹 Specify Number of Lines
You can control exactly how many lines tail outputs using the -n (number) option followed by a count. The command tail -n 20 file.log displays the last 20 lines. A common shorthand is to omit the -n and use just a hyphen followed by the number (e.g., tail -50 file.log). You can also specify a starting point with a plus sign: tail -n +11 file.log outputs everything from line 11 to the end, effectively skipping the first 10 lines. This precision is key for extracting specific sections from files, such as omitting headers or isolating recent error blocks in diagnostics.
# View last 5 lines
tail -n 5 numbers.txt
# Shorthand notation
tail -5 numbers.txt
# View last 15 lines
tail -n 15 numbers.txt
Output (last 5 lines):
16 17 18 19 20
🔹 Follow Mode (Real-time)
The -f (follow) option is one of tail's most powerful features for real-time monitoring. When invoked as tail -f application.log, the command initially prints the last 10 lines and then continues running, appending new lines to the display as they are written to the file. This is essential for live debugging, watching web server access logs, or monitoring ongoing processes. For even better persistence, use --follow=name -F (capital F), which will track a file by name, reopening it if it is rotated or recreated—a common scenario with log rotation utilities like logrotate.
# Watch log file in real-time
tail -f /var/log/syslog
# Follow with line count
tail -n 20 -f application.log
# Press Ctrl+C to stop following
Common Use Cases:
- Monitoring web server logs
- Watching application debug output
- Tracking system log messages
- Observing file changes during development
🔹 View Multiple Files
Tail can simultaneously monitor the ends of several files, which is invaluable for comparative log analysis. When you specify multiple filenames (e.g., tail -f /var/log/auth.log /var/log/syslog), tail outputs a header line like ==> /var/log/auth.log <== before each file's content. This clear visual separation allows you to watch events unfold across different system or application logs in a single terminal window. Combining this with the -f flag creates a powerful live dashboard for servers or applications that write to multiple log files, helping correlate events and errors across different services in real-time.
# Create multiple files
echo -e "Log 1\nEntry A\nEntry B" > log1.txt
echo -e "Log 2\nEntry X\nEntry Y" > log2.txt
# View both files
tail log1.txt log2.txt
Output:
==> log1.txt <== Log 1 Entry A Entry B ==> log2.txt <== Log 2 Entry X Entry Y
🔹 Skip Lines from Start
Using a plus sign (+) with the -n option changes tail's behavior to skip a specified number of initial lines. For example, tail -n +101 data.csv outputs all lines starting from line 101 to the end of the file. This is exceptionally useful for bypassing fixed-length file headers, ignoring preamble text, or processing only a subset of data from a large file. It effectively turns tail into a tool for viewing the "middle" or latter part of a file from an arbitrary starting point, complementing the head command. This technique is common in shell scripts for data extraction and automated reporting where consistent header lines must be excluded.
# Show from line 5 to end
tail -n +5 numbers.txt
# Skip first 10 lines
tail -n +11 file.txt
Output (from line 5):
5 6 7 ... 20
🔹 Useful Tail Options
Beyond core viewing functions, tail provides several specialized options to handle diverse real-world scenarios. The -c (bytes) option outputs the last N bytes instead of lines, useful for binary files or specific-sized blocks. The -q (quiet) flag suppresses filename headers when viewing multiple files. The -v (verbose) flag always prints headers, even for a single file. Options like --pid=PID can terminate tail when a specific process dies. Combining these—for instance, tail -F -n 0 -q *.log to quietly follow multiple logs from their current end—creates tailored solutions for system monitoring, data streaming, and log analysis pipelines.
Common Options:
- -n N - Display last N lines
- -f - Follow file updates in real-time
- -F - Follow and retry if file is recreated
- -q - Quiet mode, no file headers
- -v - Always show file headers
- -c N - Display last N bytes instead of lines
- --pid=PID - Stop following when process dies
# Follow and retry if file rotates
tail -F /var/log/app.log
# Show last 100 bytes
tail -c 100 file.txt
# Quiet mode (no headers)
tail -q file1.txt file2.txt