How to Tail Log Files and Keep Tailing them When the Latest One Changes Date and Time?

In Linux, the “tail” command allows users to view the end of a text or log file in the terminal. It is utilized to display the last lines of a particular file or a stream of data. It is beneficial for monitoring logs or other files which are frequently being updated with date and time.

This article will offer the step-by-step procedure to tail a log file in Ubuntu.

  • How to Tail a Log File and Keep Tailing it When the Latest One Changes Date and Time?
  • Display the Last 10 Lines of the “syslog” File
  • Display Log File Runs Every 2 Seconds

How to Tail a Log File and Keep Tailing it When the Latest One Changes Date and Time?

In Linux, it is often necessary to monitor log files to track system events, errors, and other important information. However, if you want to keep tailing a log file when the latest date of one changes, users need to follow the below procedure:

Basic Syntax

The basic syntax for the tail command to tail a log file and keep tailing it when the latest date of one changes:

$ tail [options] [log_file]

In the above syntax, the “tail” command allows users to view the end of a “log_file” in real-time.

Some common “options” for the “tail” command are given below:

  • n: Specifies the number of lines to display (e.g., tail -n 10 filename displays the last ten lines of the file).
  • -f: Monitors the file and displays any new lines that are added in real-time (useful for monitoring log files).
  • -q: Suppresses any headers or footers that would normally be displayed.

Example 1: Display the Last 10 Lines of the “syslog” File

First, users need to determine the name of the log file to tail. This can be done using the “ls” command to list the contents of the “/var/log” directory where the log files are stored:

$ ls /var/log

Once users have the name of the log file, use the “tail” command to view the end of the file in real-time. For example, to display the last 10 lines of the “syslog” file, use the following command:

$ tail -n 10 /var/log/syslog

It displays the last 10 lines of the file and continues to tail the file, updating the output whenever new lines are added.

Example 2: Display Log File Runs Every 2 Seconds

To keep tailing the log file when the latest date of one changes, users can use the watch command. This command runs a command repeatedly and displays the output in real-time. For instance, tail a log file and keep tailing it when the latest date of one change, use the following command:

$ watch -n 2 'tail -n 10 /var/log/syslog'

It runs the tail command every 2 seconds and displays the last 10 lines of the log file. If you want to stop tailing the log file, use the “Ctrl+C” command to interrupt the watch command.

To explore more examples of the “tail” command, follow our article “tail Command in Linux

Conclusion

Users can monitor log files in real-time and keep tailing them even when the latest date of one changes by following the “tail [options] [log_file]” syntax. This command runs a command repeatedly and displays the output in real-time. This can be a useful technique for tracking system events and troubleshooting issues in Linux.

This guide has explained the procedure to tail a log file and keep tailing it in Linux.