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