Home >Operation and Maintenance >Linux Operation and Maintenance >Common commands for viewing logs in Linux

Common commands for viewing logs in Linux

藏色散人
藏色散人Original
2019-04-24 16:32:05150482browse

Commands to view logs: 1. tail command, for example "tail -n 10 test.log" to query all logs after 10 lines; 2. head command, for example "head -n 10 test.log" to query The first 10 lines of logs in the log file; 3. cat command; 4. sed command.

Common commands for viewing logs in Linux

1.linux Common commands for viewing logs

tail:

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

tail -100f test.log Real-time monitoring of 100 lines of logs

tail -n 10 test .log Query the last 10 lines at the end of the log;

tail -n 10 test.log Query all the logs after the 10th line;

head:

It is the opposite of tail, tail is the number of log lines after looking at it; 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 of the log file;

cat:

tac is viewed in reverse order, and the word cat is written in reverse; example As follows:

cat -n test.log |grep "debug" Query the log of keywords

2. Application scenario 1: View by line number---Filter Get the logs near the keyword

1) cat -n test.log | grep "debug" 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 view the logs of the 10 lines before and 10 lines after the keyword:

tail -n 92 means querying the lines after line 92 Log

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

3. Application scenario 2: Query the log based on date

sed -n '/2014-12-17 16:17:20/,/2014-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;

First grep '2014-12-17 16:17:20' test.log to determine whether the time exists in the log Click

4. Application scenario three: The log content is very large, and it is inconvenient to view it when printed on the screen

(1) Use more and less Command,

such as: cat -n test.log |grep "debug" |more This will print in pages, click the space bar to turn pages

(2) Use>xxx.txt 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 the detailed content of Common commands for viewing logs in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn