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,viWait for the editor command
  • cat: Displays the content starting with the first line and outputs all the content
  • tac: Displays the content in reverse order from the last line, and outputs everything
  • more: Displays file contents one page at a time according to the window size
  • lessAnd:moreSimilar, but more powerful
  • head: Displays only the first few lines
  • tail: Displays only the last few lines

If you want to go online to see the log, only recommendedless,tail,headOf 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.

lessOpen the file

  • less +100g xx.log: Navigate directly to line 100
  • less +GG xx.log: Directly locate the last line
  • less -i xx.log: Ignores search case
  • less -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 string
  • n: Repeat the previous search
  • N: Repeat the next search
  • b: Turn the page forward
  • d: Turn back half a page
  • q: out of less
  • p n%: to jumpn%place
  • pageup,pagedown: Page up and down
  • g: Moves to the first line
  • G: Moves to the last line
  • less filenameOpen it up and thenF, can be achieved similarlytail -fThe effect of real-time view log.

uselessNote: Do not press the commandvBecause this will automatically call the system’s text editor if the system default isvimIf so, read the entire file directly into memory, remember that.

tailandhead

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.