Common commands for viewing logs in Linux

Source: Internet
Author: User

Common commands for viewing logs in Linux
1. Common command for viewing logs: tail:-n indicates the display line number; equivalent to nl command; example: tail-100f test. log monitors 100 lines of logs in real time.

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

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

Head:

This is the opposite of tail. tail is the number of lines of logs after reading. The example is as follows:

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

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

Cat:

Tac is a reverse query of cat words. The example is as follows:

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

2. Application Scenario 1: View by row number --- filter logs near keywords

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

2) cat-n test. log | tail-n + 92 | head-n 20 select the middle line of the keyword. Then, view the logs of the first 10 rows and the last 10 rows of the Keyword:

Tail-n + 92 indicates querying logs after 92 rows

Head-n 20 indicates that the first 20 records are queried in the previous query results.

3. Application Scenario 2: Query logs by date

Sed-n'/16:17:20/,/16:17:36/P' test. log

Note: The preceding two dates must be printed logs; otherwise, they are invalid;

Grep '2017-12-17 16:17:20 'test. log to check whether the time point exists in the log.

4. Application Scenario 3: log Content is too large to be viewed on the screen.

(1) Use the more and less commands,

For example, cat-n test. log | grep "debug" | more is printed by page. Click the Space key to flip the page.

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

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.