Home > Text Processing & Search > tail

tail: Displaying the End of Files and Real-time Monitoring

The tail command is used to display the last part of a file. It is commonly used to check the latest content of log files or to monitor file changes in real-time using the -f option. The -n option is particularly useful for specifying the number of lines to output, allowing you to view only a specific number of the last lines.

Overview

The tail command outputs the end portion of a file to standard output. By default, it displays the last 10 lines, and the number of lines to output can be adjusted using the -n option. It is a very powerful tool for real-time log monitoring.

Key Features

  • Display the last N lines of a file
  • Real-time monitoring of file changes (-f)
  • Process multiple files simultaneously

Key Options

The main options for the tail command control the output format and monitoring capabilities.

Output Control

Monitoring

Other

Generated command:

Try combining the commands.

Description:

`tail` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various examples of how to use the tail command.

View the last 10 lines of a file

tail -n 10 /var/log/syslog

Outputs the last 10 lines of the specified file.

View the last 50 bytes of a file

tail -c 50 /etc/passwd

Outputs the last 50 bytes of the specified file.

View from the 3rd line to the end of a file

tail -n +3 my_file.txt

Outputs all content from the 3rd line to the end of the file, including the 3rd line.

Real-time monitoring of a log file

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

Outputs new content to the log file as it is added, allowing for real-time monitoring.

Real-time monitoring robust to log rotation

tail -F /var/log/myapp/error.log

Continues to track the log file even if it is renamed or recreated. Suitable for environments with log rotation.

View the last 5 lines of multiple files

tail -n 5 file1.txt file2.txt

Outputs the last 5 lines of each of the specified files, including file name headers.

Real-time filtering of specific logs with grep

tail -f /var/log/auth.log | grep "Failed password"

Monitors a log file in real-time and filters to output only lines containing the specific keyword ('Failed password').

Tips & Considerations

Tips and points to consider for more effective use of the tail command.

Useful Combinations

  • `tail -f` is essential for log monitoring. It becomes more powerful when piped (|) with `grep` to filter specific keywords.
  • `tail -F` provides more stable log tracking than `-f` in environments with frequent log rotation.
  • The `-n` option defaults to 10 lines if omitted. `tail file.log` is equivalent to `tail -n 10 file.log`.

Performance Considerations

When using `tail -n +NUM` on very large files, it may impact performance as it needs to read from the beginning of the file up to the specified line. In such cases, other tools like `sed` or `awk` might be more efficient.


Same category commands