Home  >  Article  >  Operation and Maintenance  >  What are the three commands to view logs in Linux?

What are the three commands to view logs in Linux?

青灯夜游
青灯夜游Original
2023-02-23 11:39:4624536browse

There are three commands for viewing logs in Linux: 1. tail command, which can monitor logs in real time, with the syntax "sudo tail parameter log file path"; 2. multitail command, which can monitor and track multiple log files in real time, and also Allows users to navigate back and forth in monitored files; 3. The lnav command can monitor multiple log files in real time, and can watch and track multiple files and display their contents in real time.

What are the three commands to view logs in Linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Three commands to view logs in real time on Linux

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

What are the three commands to view logs in Linux?

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

What are the three commands to view logs in Linux?

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

What are the three commands to view logs in Linux?

View the last two lines of log

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

What are the three commands to view logs in Linux?

Multiple monitoring logs

3. lnav command - real-time monitoring of multiple log files

Another 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

What are the three commands to view logs in Linux?

lnav - Real-time log monitoring

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What are the three commands to view logs in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Can linux run exe files?Next article:Can linux run exe files?