Linux view log command
The first one: view the log of real-time changes
most commonly used:
tail -f app.log (the last 10 lines by default, equivalent to adding parameter -n 10)
tail -200f app.log (the last 200 lines, pushed forward at a certain moment)
Ctrl+c is to exit the tail command
Other cases:
tail -n 20 app.log (displays the last 20 lines of app.log)
tail -n +5 app.log (display files starting from line 5)
The second method: search for logs near keywords
The most commonly used: cat -n filename |grep “keywords”
Other cases:
cat app.log | grep -C 5 ‘Keywords’ (Display the line matching the string in the log and 5 lines before and after)
cat app.log | grep -B 5 ‘Keywords’ (display the matching string and the first 5 lines)
cat app.log | grep -A 5 ‘Keywords’ (display the matching string and the last 5 lines)
The third type: Enter the editor to find: vi (vim)
- Enter vim editing mode: vi app.log
- Enter “/keyword” and press enter to search
- To find the next one, press “n” (n to find the previous error log N to find the next error log)
4.
ctrl+f: Scroll down one screen.
ctrl+b: Turn up one screen.
ctrl+d: scroll down half a screen.
ctrl+u: Turn up half a screen.
ctrl+e: Scroll down one line.
ctrl+y: Scroll up one line.
5, ctrl+o to return to the search place
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 one
The fourth type: View all keyword-related logs (more is similar to vi)
more app.log opens all by default, from front to back
Commonly used are H (get help information), Enter (roll down a line), space (scroll down one screen), Q (quit command)
- more app.log | grep ‘keyword’
- Press the Space key: display the content of the next screen of the text
- Press the B key: display the content of the previous screen;
- Press the H key: display the help screen, and there are relevant help information on the screen;
The fifth is to view according to time
cat xxx.log |sed -n '/2020-10-24 22:16:21/,/2020-10-24 22:16:59/p' abc.log