Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the linux command application scenarios for viewing logs?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you to view the log of the linux command application scenarios, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

The linux commands for viewing logs are: tail command, head command. For example, you can use "tail-n + 10 test.log" to query all logs after 10 lines; you can use "head-n 10 test.log" to query the first 10 lines of logs in the log file.

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 monitor 100 lines of logs tail-n 10 test.log query the last 10 lines 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 queries the first 10 lines of logs in the log file; head-n-10 test.log queries all logs in the log file 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

Application scenario 1:

View by line number-filter out 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.

Application scenario 2:

Query log 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

Application scenario 3:

The content of the log is so much that 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

The above is to view the log of the linux command application scenario what all the content, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report