Home Operation and Maintenance Linux Operation and Maintenance Three commands to view logs in Linux

Three commands to view logs in Linux

Jan 04, 2023 pm 02:00 PM
linux log

The three commands for viewing logs in Linux are: 1. tail command, which can view changes in file content and log files in real time; 2. multitail command, which can monitor multiple log files at the same time; 3. , less command, which can quickly view log changes without cluttering the screen.

Three commands to view logs in Linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What are the three commands to view logs in Linux?

3 ways to view logs in real time in Linux

I recently purchased a cloud server from cnaaa.com.

We all should know how to view files in Linux, for example, you can use the cat or less command.

This is fine for viewing static files. Log files are dynamic and their contents can change at any time. To monitor log files, you need to be able to see in real time when the contents of the log files change.

So how to view the log file in real time? The tail command is available. In addition, there are other tools. This article will introduce these tools that can view log files in real time.

1. Use the tail command to view the log file

The tail command is very widely used, so system administrators often use the mantra tail the log file (ie: tail the log file) .

In most cases, the tail command is used to view the content at the end of the file, so it is named tail.

Use the -f option to track the content at the end of the file, which means it will continue to display newly added content to the file.

tail -f location_of_log_file
Copy after login

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-nzCWCjye-1664183028850) (D:/img/640.png)]

To stop tracking the log file, you can use the ctrl c shortcut key.

tail and grep

As mentioned above, the tail command can view changes in file content in real time. However, when the file content is updated very quickly, the newly updated content flashes by. In this case, it is not so convenient to view.

For example, when we track log files, we often monitor a specific term (string), which is very inconvenient to track in a large amount of rapidly updated content.

To solve this problem, we can combine the tail and grep commands. As shown below:

tail -f log_file | grep search_term
Copy after login

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-LxXIylsU-1664183028854)(D:/img/640-1664179747239 -1.png)]

It looks much better this way, right? On this basis, let us make some improvements.

Use grep to display search terms. The information displayed is relatively limited. It only displays the search results, so we often use the -C option to display the first and last few lines of the search results:

tail -f log_file | grep -C 3 search_term
Copy after login

In this way, we You can see several lines of information before and after the search results, and you can better track the log information.

Do you want to make some improvements? You can use grep for multiple search terms, and then case-insensitively:

tail -f log_file | grep -C 3 -i - E 'search_term_1|search_term_2'
Copy after login

Use log rotation (log rotation) to track logs

Most enterprise servers, logs will be rotated (rotation), that is When the log file reaches a certain size, it is renamed and compressed.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-h2RUcofL-1664183028856) (D:/img/640-1664179747241-2.png) ]

This can cause problems if log files are tracked in real time. By default, the tail command works on file descriptors. If the current log file is rotated, the tail command will now point to an archived log file, which will now log no changes.

The solution is to track the log file by its name. This way, even if log rotation occurs, the tail will point to the current log file (since its name never changes).

tail --follow=name log_file | grep -C 3 -i - E 'search_term_1|search_term_2'
Copy after login

tail is very suitable for real-time monitoring of log files, but the above method only monitors one log file. What if you want to monitor multiple log files? Please see the next section.

Use tail to view multiple log files

Working in a Linux system, you can use the tail command to monitor multiple log files at the same time. You only need to provide the path of the file:

tail -f log_file_1 -f log_file_2
Copy after login

With the above command, you will see the update of the log file in real time, and the file name will be in front to distinguish different log files:

[External link image transfer failed, the source site may have anti-leeching mechanism, it is recommended to save the image and upload it directly (img-CMiWKszs-1664183028859)(D:/img/640-1664179747242-3.png)]

In addition to the above method, there is another more convenient way , just use a tool called multitail.

2. Use multitail to monitor multiple log files at the same time

As the name suggests, multitail is used to display multiple files at the same time.

Since tail can monitor multiple files at the same time, what is so special about multitail?

The beauty of multitail is that it can display files in split view and even display different files in different rows and columns.

tail 在同一视图中显示所有内容,所以有时候很难跟踪,multitail 通过提供类似 screen 命令的分割视图来克服了这一困难。

注意,multitail 在大多数Linux系统中没有被默认安装,所以在使用前需要先手动安装。

在 multitail 命令后跟文件路径,最好一次不要超过3个,因为超过3个或以上,跟踪起来就比较困难了。

multitail log_file_1 log_file_2
Copy after login

默认情况下,multitail 的工作方式与 tail -f 相同,它显示最后100行,然后进入实时监视视图;另外,它按行来拆分视图。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EZ9iX4d3-1664183028861)(D:/img/640-1664179747242-4.png)]

你可以按 b 键打开一个文件选择窗口,选择某个日志文件查看,以做进一步分析。

分割视图使用 -s 选项,后面跟一个数字,即视图的数量:

multitail -s 2 log_file_1 log_file_2
Copy after login

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ETQjV0KA-1664183028863)(D:/img/640-1664179747243-5.png)]

按 q 键可退出 multitail 所有的视图。

multitail 可以做的还有很多,大家感兴趣可以查看一下它的官方文档,本文就不继续介绍了。

3. 使用 less 命令实时查看日志文件

less 命令多用于读取文本文件,也可用于读取实时被更改的文件。

选项 +F 可以实时跟踪文件的更改:

less +F log_file
Copy after login

上述命令会打开日志文件,并实时显示正在写入的更改:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oDZivgss-1664183028865)(D:/img/640-1664179747243-6.png)]

按 ctrl +c 中断显示,按 q 会退出视图。

与 tail 命令不同,此方法可以让我们快速查看日志的更改,而不会使屏幕混乱。

上述监视日志的方法适用于传统的基于文本的日志文件。对于系统日志,可以使用 syslogs,但是现在许多 Linux 发行版已经开始使用 journal 日志来查看和分析日志,所以需要使用 journalctl 命令。

推荐学习:《Linux视频教程

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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

How to install centos How to install centos Apr 14, 2025 pm 09:03 PM

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

What are the backup methods for GitLab on CentOS What are the backup methods for GitLab on CentOS Apr 14, 2025 pm 05:33 PM

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

How to mount hard disk in centos How to mount hard disk in centos Apr 14, 2025 pm 08:15 PM

CentOS hard disk mount is divided into the following steps: determine the hard disk device name (/dev/sdX); create a mount point (it is recommended to use /mnt/newdisk); execute the mount command (mount /dev/sdX1 /mnt/newdisk); edit the /etc/fstab file to add a permanent mount configuration; use the umount command to uninstall the device to ensure that no process uses the device.

What to do after centos stops maintenance What to do after centos stops maintenance Apr 14, 2025 pm 08:48 PM

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.

See all articles