Current location - Music Encyclopedia - Chinese History - How to View Logs in linux
How to View Logs in linux
Methods/steps

You must first understand two basic commands:

Tail? -n? 10? test.log? Query the log of the last 10 line at the end of the log;

tail -n + 10 test.log? Query all logs after line 10;

head -n 10? test.log? Query the first 10 lines of the log file;

head -n - 10? test.log? Query all logs in the log file except the last 10 line;

Scene 1: View by line number-filter out logs near keywords.

Because we usually get few logs with grep, we need to check the nearby logs.

That's what I did. First of all: cat -n test.log |grep "terrain"? Gets the line number of the key log

& lt3> gets the line number of "Terrain" keyword as 102. At this time, if I want to check the logs of the first 10 line and the last 10 line of this keyword:

cat-n test . log | tail-n+92 | head-n 20

Tail -n +92 represents the log after querying 92 lines.

Head -n 20 represents the top 20 records in the query results before checking.

Scenario 2: So how do you check by date? Usually, we really need to find the log at the specified time.

sed-n '/20 14- 12- 17 16: 17:20/,/20 14- 12- 17 16: 17:36/p '? Test log

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

For date printing, you can first grep' 2014-12-1716:17: 20' test.log to determine whether there is this time point in the log, so as to ensure that the log can be obtained in step 4.

Querying logs according to time periods is a very useful command.

If we find a lot of logs, it is not convenient to print them on the screen. There are two ways:

(1) Use more and fewer commands, such as: cat-ntest.log | grep "terrain" | more, so that it will print by page. Click the spacebar to turn pages.

(2) use > Xxx.txt to save it in a file, and then you can drop down this file for analysis. For example:

Cat -n test.log |grep "terrain"? & gtxxx.txt

These log viewing methods should be able to meet daily needs.