Article From:

(1)LinuxDirectory operation command

cd ..Exit the current directory, return to the previous directory; CD / Exit the current directory, return to the root directory;

mkdirThe command is used to create a new directory; the rmdir command function deletes the specified empty directory.

(2)LinuxFilter logs

The basic commands listed below are very useful for inquiring about online and offline problems. You might as well learn from them.

Two basic commands must be understood:

tail  -n  10  test.log   Query the logs of the last 10 lines at the end of the log;

tail -n +10 test.log    Query all logs after 10 rows;

head -n 10  test.log   Query the first 10 lines of the log file;

head -n -10  test.log   Query all logs except the last 10 lines of the log file;

Scenario 1: View by line number – filter out the logs near keywords

Because we usually get very few logs with grep, we need to check the nearby logs.

First, cat-n test. log | grep “Terrain” gets the line number of the key log.

<3>Get the line number of the “terrain” keyword is 102 lines. If I want to view the logs of the first 10 lines and the last 10 lines of the keyword at this time:

cat -n test.log |tail -n +92|head -n 20

tail -n +92Represents the log after 92 rows of query

head -n 20 Represents looking up the first 20 records in the previous query results

Scenario 2: So how do we look up by date? Usually we need to look up the log at the specified time end very much.

sed -n ‘/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p’  test.log

In the second way, commands between two line numbers are:

sed -n ‘5,10p’ filename So you can just look at lines 5 to 10 of the file.

Special note: The above two dates must be printed in the log, otherwise they will not be valid.

For date printing, grep’2014-12-17 16:17:20’test.log can be used to determine whether there is a time point in the log to ensure that the log is available in step 4.

This is a very useful command for querying logs based on time periods.

If we look up a lot of logs and print them on the screen is not easy to view, there are two ways:

(1)Use more and less commands, such as cat-n test.log | grep “Terrain” | more, to print pages, and turn pages by clicking the space bar.


moreCommand, similar to cat, cat command is the content of the entire file displayed on the screen from top to bottom. More will be displayed page by page to facilitate users to read page by page, and the most basic instruction is to press the blank key (space) to display on the next page, press the B key will go back (b)Ack) is displayed on one page and has the function of searching for strings. The more command reads the file from front to back, so the entire file is loaded at startup.


less Tools are also tools for paging and displaying files or other outputs. It should be said that Linux is an orthodox tool for viewing the contents of files, and its functions are extremely powerful. Less is more flexible than more. At more, we can’t turn forward, we can only go forward.Look back, but when less is used, you can use buttons like [pageup] pagedown to look back and forth at the file, which is easier to use to view the content of a file! In addition, there are more search functions in less.You can search not only downwards, but also upwards.


(2)Save it in a file using & gt; xxx. txt, and then pull down the file analysis. For example:

cat -n test.log |grep “Terrain “gt; xxx. txt”

These log viewing methods should be able to meet daily needs.

grepCommand multi-condition query

1、Or operation

 grep -E ’123|abc’ filename  // Find rows that contain 123 or ABC in a file
 egrep ’123|abc’ filename    // The same can be achieved with egrep
 awk ’/123|abc/’ filename   // awk Ways of realization

2、and operation

grep pattern1 files | grep pattern2 :Displays rows that match both pattern 1 and pattern 2.

3、Other operations

grep -i pattern files :Search case-insensitive. Case-sensitive by default,
grep -l pattern files :List only matching file names.
grep -L pattern files :List mismatched file names,
grep -w pattern files :Match only the whole word, not part of the string (for example,’magic’instead of’magical’).
grep -C number pattern files :Matched contexts show [number] rows separately

grepThe – A, B and C options can show how many lines are matched after, before and after:

grep -A 100 ‘TooManyResultsException’ catalina.log.2017-09-25  after

grep -B 100 ‘TooManyResultsException’ catalina.log.2017-09-25  Front

grep -C 100 ‘TooManyResultsException’ catalina.log.2017-09-25  around

grep -C 100 –color ‘TooManyResultsException’ catalina.log.2017-09-25  Color output

Query Log Special Scenario:

If the logs are very many, there are thousands or thousands of data in a short hour. It is very troublesome to filter only according to the conditions, even if there are many data to be filtered out. If we know when this call occurs, we can determine which ones are based on the time when the log is first printed.Records meet query requirements. So, we can use the date of the log as the search criterion, and cooperate with grep, as follows:

sed -n ‘/2017-03-08 15:42:03/,/2017-03-08 15:42:05/p’ dubbo-access-consumer.2017-03-08.log | grep countOrgOrder

We can use the query mode of sed command:

Queries using patterns
     [root@localhost ruby] # sed -n ‘/ruby/p’ ab    #Queries include all rows where the keyword Ruby resides
     [root@localhost ruby] # sed -n ‘/\$/p’ ab        #Queries include all rows where the keyword $is located, using backslash \ masking special meanings

Query for log files of type. bz2, as follows: bzgrep 60000005137 dubbo-access-provider. 2017-05-17.log.bz2

Here’s how to query the processes running on the filter server:

1.If you want to query all running processes on the server, you can use the command PS aux to find out.

2.If there are specific screening conditions, you can use PS aux | grep xxx.

3.Or use the command ps-ef | grep XXXX to complete the corresponding screening work.

4.The command kill-9 XXXXX XXXXX to terminate a process is the sequence number found above, such as: 19979 thread termination: kill-9 19979


Link of this Article: Linux Log Screening Command

Leave a Reply

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