Home  >  Article  >  Operation and Maintenance  >  What is the linux command to view logs?

What is the linux command to view logs?

青灯夜游
青灯夜游Original
2020-08-24 11:37:514394browse

The Linux commands for viewing logs are: tail command and head command. Example: You can use "tail -n 10 test.log" to query all logs after the 10th line; you can use "head -n 10 test.log" to query the first 10 lines of logs in the log file.

What is the linux command to view logs?

Common commands to view logs

tail:

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

tail -100f test.log      实时监控100行日志
tail  -n  10  test.log   查询日志尾部最后10行的日志;
tail -n +10 test.log    查询10行之后的所有日志;

head:

is the opposite of tail, tail is to look after How many lines of logs; examples are as follows:

head -n 10  test.log   查询日志文件中的头10行日志;
head -n -10  test.log   查询日志文件除了最后10行的其他所有日志;

cat:

tac is viewed in reverse order, which means the word cat is written in reverse order; examples are as follows:

cat -n test.log |grep "debug"   查询关键字的日志

Application scenario one:

View by line number---filter out logs near keywords

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 log of the first 10 lines and the 10 lines of this keyword:

TAIL -N 92 indicates that the log of the query 92 lines

HEAD -N 20 means that you can check in the query results in the previous query The first 20 records

Application scenario two:

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 two dates above must be the logs printed in the log, otherwise they will be invalid;

                                       First grep '2014-12-17 16:17:20' test.log to determine whether there is this time point in the log

Application scenario three:

The log content is very large, and it is inconvenient to view it when printed on the screen.

(1) Use the more and less commands,

For example: cat -n test.log |grep "debug" |more This way Page printing is done, click the space bar to turn pages

(2) Use >xxx.txt to save it to a file. You can then pull down the file for analysis

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

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is the linux command to view logs?. 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