Linux log Query Common commands

Source: Internet
Author: User

First, tail command syntax

tail [-f] [-c number |-N number |-M number |-B number |-K number] [File]
Parameter explanation:
-F This parameter is used to monitor file growth.
-c number reads the specified file from the number byte location
-N number reads the specified file from the number line position.
-m number reads the specified file from the multiple-byte character position, for example, if your file assumes that you include Chinese characters, it is assumed that specifying the-c parameter may result in truncation, but using-m avoids the problem.
-B number reads the specified file from the 512-byte block position represented by number.
-K number reads the specified file from the 1KB block position represented by number.
file specifies the object name of the operation
In the above command, the number is involved, assuming that it is not specified, 10 rows are displayed by default. Number is preceded by a sign that indicates whether the offset is calculated from the top or from the tail.
Tail can run files are generally below/usr/bin/.

Second, tail command use method demonstration example

1. Tail-f filename
Description: Monitors the trailing contents of the filename file (the default 10 lines, which is equivalent to increasing the parameter-n 10), and refreshes the display on the screen. Exit, press CTRL + C.

2. Tail-n filename
Description: Displays the last 20 lines of the filename.

3. Tail-r-N-ten filename
Description: The last 10 lines of filename are displayed in reverse order.

Add:
Commands similar to the tail feature are:
Cat starts displaying the contents of the archive from the first line.
The TAC starts by displaying the contents of the archive from the last line.
More paging displays the contents of the archive.
Less is similar to more but supports page forward
Head only shows the previous lines
Tail only shows the following lines
N Show file contents with line number
OD Displays the contents of the file in binary mode

Scene:

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

Because we usually get a few logs with grep, we need to look at the logs nearby.

That's what I did, first: cat-n test.log |grep "terrain" gets the line number of the critical log

<3> get the "terrain" keyword where the line number is 102 rows. At this point if I want to see the first 10 lines of this keyword and the last 10 lines of the log:

Cat-n Test.log |tail-n +92|head-n 20

Tail-n +92 indicates a log after querying 92 rows

Head-n 20 indicates that the first 20 records are checked in the previous query results.

Scenario 2: So what do we do by date? Usually we really need to find the logs at the specified time end.

Sed-n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p ' Test.log

Special Note: The above two date must be the log printed in the log, otherwise invalid.

    1. For date printing, you can first grep ' 2014-12-17 16:17:20 ' test.log to determine if there is a point in the log to ensure that the 4th step can get the log

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

    2. If we look for a lot of logs, printing on the screen is not easy to view, there are two ways:

      (1) Use the more and less commands, such as: Cat-n test.log |grep "terrain" |more so that the page printing, by clicking the Space bar to page

      (2) using >xxx.txt to save it to a file, you can pull down this file analysis. For example:

      Cat-n test.log |grep "Terrain" >xxx.txt

Linux log Query Common commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.