How to view the contents of log files in real time in Linux? Well, there are many utilities that help users output the contents of a file when it changes or is continually updated. A common command in Linux to display file contents in real time is the tail command (manage files efficiently).
1. tail command - real-time monitoring of logs
As mentioned above, the tail command is the most common solution for displaying log files in real time. However, there are two versions of the command that displays the file, as shown in the example below.
In the first example, the command tail
requires the -f
parameter to trace the contents of the file.
$ sudo tail -f /var/log/apache2/access.log
Real-time monitoring of Apache logs
The second version of this command is actually a command itself: tailf
. You don't need to use the -f
switch because the command is built-in with the -f
parameter.
$ sudo tailf /var/log/apache2/access.log
Real-time Apache log monitoring
Typically, the logrotate utility rotates log files frequently on a Linux server . To view the rotated log files on a daily basis, you can use the tail -F
command.
tail -F
will track new log files being created and start tracking new files instead of old files.
$ sudo tail -F /var/log/apache2/access.log
However, by default, the tail command will display the last 10 lines of the file. For example, if you only want to view the last two lines of a log file in real time, use the -n
file combined with the -f
flag, as shown in the example below.
$ sudo tail -n2 -f /var/log/apache2/access.log
View the last two lines of logs
2.multitail command - monitor multiple log files in real time
Another interesting command for displaying log files in real time is the multitail
command. The name of the command means that the multitail
utility can monitor and track multiple files in real time. Multitail also allows you to navigate back and forth among monitored files.
To install mulitail utility in Debian and RedHat based systems, issue the following command.
$ sudo apt install multitail [On Debian&Ubuntu] $ sudo yum install multitail [On RedHat&CentOS] $ sudo dnf install multitail [On Fedora 22+ version]
To display the output of two log files simultaneously, execute the command shown in the following example.
$ sudo multitail /var/log/apache2/access.log /var/log/apache2/error.log
Multiple monitoring logs
3. lnav command - real-time monitoring of multiple log files
Another An interesting command, similar to the multitail command, is the lnav command. The Lnav utility can also watch and track multiple files and display their contents in real time.
Install the lnav utility in Debian and RedHat based Linux distributions by issuing the following command.
$ sudo apt install lnav [On Debian&Ubuntu] $ sudo yum install lnav [On RedHat&CentOS] $ sudo dnf install lnav [On Fedora 22+ version]
Observe the contents of two log files simultaneously by issuing the command, as shown in the example below.
$ sudo lnav /var/log/apache2/access.log /var/log/apache2/error.log
lnav - real-time log monitoring
4. less command-display real-time output of log files
Finally, you can use the less command to display live output of a file if you type Shift F
.
Like the tail utility, Shift F
pressing less
while in an open file will start after the file ends. Alternatively, you can start entering live viewing files with fewer F
flags.
$ sudo less + F /var/log/apache2/access.log
Use less command to monitor logs
The above is the detailed content of Detailed explanation of four commands for viewing logs in real time on Linux. For more information, please follow other related articles on the PHP Chinese website!