Linux log view using linux command line
Linux log view, as a server development engineer is a must-have skill.
The Linux system also provides a wealth of log viewing commands to deal with different usage scenarios.
This article will share with you how to use the correct commands to view logs in different business scenarios.
Linux Log View Command
First, let’s take a look at the commands that can be used to view logs in Linux.
- vim/vi – text editor
- cat – print on the standard output
- more – a filter for paging through text one screenful at a time
- less – similar to more command (ubuntu)
- head – output the first part of files
- tail – output the last part of files
- grep – print lines that match patterns
All of the above linux commands can view linux logs, but they can solve different problems.
Let us see what problems each of these Linux commands can solve.
Log Viewing Examples
- Scenario 1, introduces the real-time monitoring log, that is, the newly added log part of the monitoring, and the content that has been written to the log file cannot be viewed.
- Scenario 2, describes how to view the small log file, that is, view the entire contents of the small log file;
- Scenario 3, let’s introduce to view the overall content of the large log file;
- Scenario 4, we introduce how to view the specified content of the log.
When we need to monitor linux log files in real time, we can usually use the tail command. For example, monitor Nginx access log, we can use the tail -f option to wait for the data to be written.
[email protected]:/usr/local/nginx/logs# tail -f access.log 18.104.22.168 - - [06/Jun/2020:11:20:46 +0800] "GET /ads.txt HTTP/1.1" 301 178 "-" "-" 22.214.171.124 - - [06/Jun/2020:11:21:07 +0800] "GET /robots.txt HTTP/1.1" 301 178 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0" ...
If you need to monitor the keywords specified in the log file, you can use the tail command to use the grep command to monitor specific keywords through the pipeline.
[email protected]:/usr/local/nginx/logs# tail -f access.log | grep "ads" 126.96.36.199 - - [06/Jun/2020:11:20:46 +0800] "GET /ads.txt HTTP/1.1" 301 178 "-" "-"
The tail -f command is the most commonly used command in our daily monitoring logs. It can help us track service logs.
When we view the smaller log file, we can use the cat command to print the entire content or use vim to directly edit the log file.
view log with cat command
Example 1: View the contents of the test.log file
➜ ~ cat test.log
Example 2: View the contents of the test.log file and display the line number
➜ ~ cat -n test.log
view log with vim command
The vim command is a text editor, and we can also use it when viewing smaller log files.
➜ ~ vim test.log # Show line number ➜ ~ vim test.log ... :set number
But do not use the vim command to view the contents of large files, after using the vim command, all the contents of the file will be loaded into the memory. If the memory is not large enough, it may cause the server to be paralyzed.
To view large log files, we can use more (less), which does not load the entire file into memory at once, but uses paging.
➜ ~ more go.sh
If you just want to view the header or tail part of the log file, you can use the head or tail command.
➜ ~ head -10 go.sh
To view the specified keywords in the log file, we can use the grep command, which supports searching and outputting the specified keywords in the file.
➜ ~ grep -C 3 "Success" go.sh