The tail
command in Unix is a powerful utility designed to print the last few number of lines (10 lines by default) of a certain file, then terminates.
What is the tail
Command in Unix?
The tail
command is a fundamental Unix-like operating system utility used for displaying the latter part of a file or piped data. It is most commonly employed to view the end of log files as they are being written, providing a real-time monitoring capability.
How the tail
Command Works
By default, when you execute the tail
command without any additional options, it will display the last 10 lines of the specified file. Once these lines are printed to the standard output, the command exits.
Example from reference:
As stated in the reference, "By default 'tail' prints the last 10 lines of a file, then exits. as you can see, this prints the last 10 lines of /var/log/messages." This illustrates its primary function: providing a quick glance at the most recent entries in a file.
Key Features and Options of tail
The tail
command comes with several useful options to customize its behavior, making it versatile for various tasks, especially when dealing with dynamic log files.
Here are some of the most frequently used options:
Option | Description | Example Command |
---|---|---|
-n NUM |
Displays the last NUM lines of the file. This allows you to specify exactly how many lines you want to see. |
tail -n 50 filename.log |
-f |
Follows the file as it grows. Instead of exiting after printing the last lines, tail -f continuously monitors the file for new additions and prints them as they appear. This is invaluable for real-time log monitoring. |
tail -f /var/log/syslog |
-c NUM |
Displays the last NUM bytes (characters) of the file instead of lines. |
tail -c 100 filename.txt |
--pid=PID |
Used with -f , it terminates tail if the process with the specified PID dies. |
tail -f access.log --pid=1234 |
--retry |
Used with -f , it keeps trying to open a file if it's inaccessible. Useful for files that might not exist yet or are temporarily locked. |
tail -f /var/log/new_app.log --retry |
Practical Examples of Using tail
The flexibility of tail
makes it indispensable for system administrators, developers, and anyone who needs to quickly inspect file content.
-
Viewing the last 10 lines of a file:
tail my_document.txt
This will output the final 10 lines of
my_document.txt
. -
Viewing a specific number of lines:
To see the last 25 lines of a web server access log:tail -n 25 /var/log/apache2/access.log
-
Real-time monitoring of a log file:
To continuously watch a system log file for new entries:tail -f /var/log/messages
This command will keep running, displaying new lines as they are appended to
/var/log/messages
. You can exit this mode by pressingCtrl+C
. -
Combining
tail -f
with other commands:
You can pipe the output oftail -f
to other commands likegrep
to filter real-time output for specific patterns:tail -f /var/log/auth.log | grep "Failed password"
This command will only show new lines in the authentication log that contain the phrase "Failed password," which is useful for monitoring security events.
Benefits of Using tail
- Quick Debugging: Allows developers and system administrators to quickly see recent errors or activities in application logs without opening large files.
- Real-time Monitoring: The
-f
option provides a live view of log files, essential for troubleshooting issues as they occur. - Efficiency: Instead of loading an entire file into memory,
tail
only processes the relevant last part, making it efficient for very large files.
The tail
command is a simple yet powerful tool for file inspection and monitoring in Unix-like environments, crucial for diagnostics and operational oversight.
[[Unix File Utilities]]