As a backend programmer, you deal with Linux in many places. If you don’t know how to read Linux logs, you are very likely to be ridiculed by colleagues and interviewers, so it is very important to master one or several methods of viewing logs. There are many commands for viewing logs in Linux: tail, cat, tac, head, echo, etc. This article only introduces several commonly used methods. 1. tail This is the way I view it most often.
Usage is as follows:
It is usually used in conjunction with grep, for example:
If the amount of data to be queried at one time is too large, you can turn the pages to view it, for example:
2. head Head is the opposite of tail. It reads the first few lines of log.
For other parameters of head, refer to tail 3. cat cat displays the first line to the last line on the screen To display the entire file at once: $ cat filename Create a file from the keyboard: $cat > filename Merge several files into one: $cat file1 file2 > file can only create new files, cannot edit existing files. Append the contents of one log file to another: $cat -n textfile1 > textfile2 Clear a log file: $cat : >textfile2 Note: > means create, >> means append. Don't get confused. For other parameters of cat, refer to tail 4. more The more command is a text filter based on the vi editor. It displays the contents of text files page by page in full screen mode and supports keyword positioning operations in vi. There are several built-in shortcut keys in the more list, the commonly used ones are H (get help information), Enter (scroll down one line), Space (scroll down one screen), Q (exit command). The more command reads the file from front to back, so the entire file is loaded at startup. This command displays one screen of text at a time, stops when the screen is full, and a prompt message appears at the bottom of the screen, giving the percentage of the file that has been displayed so far: –More– (XX%)
5. sed This command can search for a specific section of the log file, based on a time range, and can be queried by line number and time range. By line number sed -n '5,10p' filename This way you can view only lines 5 to 10 of the file. By time period sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log 6. less When querying logs with the less command, the general process is as follows
Common command parameters:
Generally, I check the logs and apply other commands
Linux log file description
The above is all the content compiled by the editor of 123WORDPRESS.COM. I hope it can help everyone. You may also be interested in:
|
<<: Detailed explanation of desktop application using Vue3 and Electron
>>: How to install MySQL database on Ubuntu
Flappy Bird is a very simple little game that eve...
Recently, the company has begun to evaluate all s...
This article records the detailed installation tu...
Table of contents Overview computed watch monitor...
Table of contents Inheritance and prototype chain...
Preface MySQL master-slave replication is the bas...
Due to your company standards, you may only allow...
Windows 10 now supports Linux subsystem, saying g...
This article example shares the specific code of ...
During system maintenance, you may need to check ...
A few days ago, I introduced to you a domestic xh...
Elasticsearch is very popular now, and many compa...
When I was printing for a client recently, he aske...
1. Server setup The remote repository is actually...
This article describes how to use docker to deplo...