linux view log

The first one: View the log of real-time changes (compared to eating memory). The most commonly used: tail -f filename (the last 10 lines are defaulted, which is equivalent to adding the parameter -n 10) Ctrl+c is to exit the tail command. Other situations: tail -n 20 filename (display the last 20 lines of filename) tail -n +5 filename (display the file from the 5th line) The second type: search for logs near keywords The most commonly used: cat -n filename |grep “

The first one: view the log of real-time changes (comparison consumes memory)

most commonly used:

tail -f filename (default last 10 lines, equivalent to adding parameter -n 10)

Ctrl+c is to exit the tail command

Other cases:

tail -n 20 filename (display the last 20 lines of filename)

tail -n +5 filename (display files starting from line 5)

The second method: search for logs near keywords

most commonly used:

cat -n filename |grep “keywords”

Other cases:

cat filename | grep -C 5 ‘Keywords’ (Display the line matching the string in the log and the 5 lines before and after)

cat filename | grep -B 5 ‘keywords’ (display the matching string and the first 5 lines)

cat filename | grep -A 5 ‘keywords’ (display the matching string and the last 5 lines)

The third type: Enter the editor to find: vi (vim)

  1. Enter vim editing mode: vim filename
  2. Enter “/keyword” and press enter to search
  3. To find the next one, press “n”

Exit: After pressing the ESC key, and then enter the : number, vi will wait for us to enter the command at the bottom of the screen

wq! Save and exit;

q! Exit without saving;

Other cases:

/Keyword Note: forward search, press n key to move the cursor to the next qualified place
?Keyword Note: For reverse search, press shift+n key to move the cursor to the next qualified

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish