Get the App
SLTechnology News&Howtos  ›  Servers  › 

What are the common commands for linux to view logs

Shulou Source: shulou.com Published: 2022-06-02 00:35:03 09月19日 Update

This article is to share with you what are the common commands for linux to view logs. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

1.linux common commands for viewing logs

Tail:

-n is the display line number; equivalent to the nl command; examples are as follows:

Tail-100f test.log real-time monitoring 100 lines of logs

Tail-n 10 test.log query the log of the last 10 lines at the end of the log

Tail-n + 10 test.log query all logs after 10 lines

Head:

Contrary to tail, tail refers to the number of lines of log after reading. Examples are as follows:

Head-n 10 test.log query the first 10 lines of logs in the log file

Head-n-10 test.log query log file all logs except the last 10 lines

Cat:

Tac is viewed in reverse order, which is the reverse of cat words. Examples are as follows:

Cat-n test.log | grep "debug" query the log of the keyword

two。 Application scenario 1: view by line number-filter the log near the keyword

1) cat-n test.log | grep "debug" to get the line number of the key log

2) cat-n test.log | tail-n + 92 | head-n 20 Select the middle line where the keyword is located. Then check the log of the first 10 lines and the last 10 lines of this keyword:

Tail-n + 92 represents the log after 92 lines of the query

Head-n 20 means to check the first 20 records in the previous query results.

3. Application scenario 2: query logs based on date

Sed-n'/ 2014-12-17 16-14-17 17-17 17-17 17-17-17 17-12-16-17 17-17-17 17-12-17 16-17-17 17-12-17 16-17-17-17-17-14-12-16-16-17-17-17-17-12-17-16-16-17-14-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-17 17-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-16-17 17-12-17 16-17 17-12-17

Special note: the above two dates must be the log printed in the log, otherwise it will be invalid

First grep '2014-12-17 16-16-14-17-17-17-17-17-17-16-17-17-17-17-16-17-17-17-16-17-17-17-16-17-12-16 to determine whether there is this time point in the log

4. Application scenario 3: the log contains a lot of content, so it is not easy to view it when printed on the screen

(1) use the more and less commands

For example: cat-n test.log | grep "debug" | more is printed in pages. Turn the page by clicking the spacebar.

(2) use > xxx.txt to save it to a file, and then you can pull down the file for analysis.

For example: cat-n test.log | grep "debug" > debug.txt

Thank you for reading! About linux view log which commonly used commands are shared here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Tags: Logs queries commands keywords files commonly used examples content scenarios line numbers applications dates more not bad practical one line two in reverse order then Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi macOS Shulou Information Shulou Technology