Abstract
Recently, there are a lot of online accidents, and the company’s monitoring system is really not working well, so in order to catch more information, I need to go to the online server to see the real-time log. As a new student, I opened jetty’s log file with Vim, and nearly 10 GIGABytes of log files were read into the memory by VIm. As a result, the server’s memory was overwhelmed, and the system automatically started the kill process. Finally, jetty’s process was killed, resulting in online failure.
So this article focuses on commands that you can use to view log files without taking up a lot of memory on Linux.
Introduction
Generally speaking, the following commands can be used to view file contents in Linux:
vim
,vi
Wait for the editor commandcat
: Displays the content starting with the first line and outputs all the contenttac
: Displays the content in reverse order from the last line, and outputs everythingmore
: Displays file contents one page at a time according to the window sizeless
And:more
Similar, but more powerfulhead
: Displays only the first few linestail
: Displays only the last few lines
If you want to go online to see the log, only recommendedless
,tail
,head
Of the three.
less
Less tool is Linux orthodox view file content tool, you can use pageUp, pageDown and other keys to scroll through the file, also can directly search the content you need, the most important is that less before viewing the entire file, it is to show which screen to load the line. The format of the less command is as follows: less [parameter] File name.
less
Open the file
less +100g xx.log
: Navigate directly to line 100less +GG xx.log
: Directly locate the last lineless -i xx.log
: Ignores search caseless -N xx.log
: Displays the line number of each line
Commonly used commands are as follows:
/
+ string: Searches down a string?
+ string: Searches up the stringn
: Repeat the previous searchN
: Repeat the next searchb
: Turn the page forwardd
: Turn back half a pageq
: out of lessp n%
: to jumpn%
placepageup
,pagedown
: Page up and downg
: Moves to the first lineG
: Moves to the last lineless filename
Open it up and thenF
, can be achieved similarlytail -f
The effect of real-time view log.
useless
Note: Do not press the commandv
Because this will automatically call the system’s text editor if the system default isvim
If so, read the entire file directly into memory, remember that.
tail
andhead
Tail and head are used to display the end or beginning number of text blocks.
For example, if you want to view the first 10 lines of a file, you can head-10 filename, and if you want to view the last 10 lines, you can tail-10 filename.
Tail can use the -f parameter to see real-time updates to the file, such as tail -f finename, to see that the file is constantly printed to the screen as it is updated.
Tail -f -n 1000 filename to view the last 1000 lines of the file in real time.