linux tail view log file methods

  • 2020-11-18 06:36:09
  • OfStack

The tail command writes files to standard output from the specified point. Using the -ES3en option of the tail command, it is easy to view the changing log files. tail-f filename displays the last part of filename on the screen and refreshes it so that you can see the latest file contents.

1. Command format;

tail[Required parameters][Select parameters][file]

2. Command function:

Used to display the contents of the end of the specified file, when not specified, as input. Often view log files.

[

-ES20en loop read
-q does not display processing information
-ES24en displays detailed processing information
-c < The number of > The number of bytes displayed
-n < The number of rows > According to the number of rows
--pid=PID and -f used together to indicate that the process ends after ID,PID, dies.
-ES40en, --quiet, --silent never prints the header of the given file name
-ES44en, -- ES45en-ES46en =S used in combination with -ES48en to indicate S seconds of sleep at intervals of each iteration

]

4. Examples of use:

Example 1: Display the end of the file

Command:

tail -n 5 /var/log/yum.log

Output:


[root@linux linux]# tail -n 5 /var/log/yum.log
Jun 29 14:45:30 Installed: samba-3.6.23-35.el6_8.i686
Jun 29 14:45:31 Installed: 2:xinetd-2.3.14-40.el6.i686
Jun 29 14:45:34 Installed: samba-swat-3.6.23-35.el6_8.i686
Jun 29 14:45:37 Updated: samba-client-3.6.23-35.el6_8.i686
Jun 29 14:45:37 Updated: libsmbclient-3.6.23-35.el6_8.i686
[root@linux linux]#

Description:

Displays the last five lines of the file

Example 2: Loop through the file contents

Command:

tail -f /var/log/yum.log

Output:


Jun 22 19:54:08 Updated: PackageKit-device-rebind-0.5.8-26.el6.i686
Jun 22 19:54:09 Updated: rpm-build-4.8.0-55.el6.i686
Jun 29 14:45:24 Updated: samba-winbind-3.6.23-35.el6_8.i686
Jun 29 14:45:24 Updated: samba-winbind-clients-3.6.23-35.el6_8.i686
Jun 29 14:45:28 Updated: samba-common-3.6.23-35.el6_8.i686
Jun 29 14:45:30 Installed: samba-3.6.23-35.el6_8.i686
Jun 29 14:45:31 Installed: 2:xinetd-2.3.14-40.el6.i686
Jun 29 14:45:34 Installed: samba-swat-3.6.23-35.el6_8.i686
Jun 29 14:45:37 Updated: samba-client-3.6.23-35.el6_8.i686
Jun 29 14:45:37 Updated: libsmbclient-3.6.23-35.el6_8.i686

The command can real-time view the log, and automatically refresh the log, let us see the latest log files, clear of their own program execution process, easy to find the problem.

supplement

use

Writes a file to standard output from the specified point. Using the -ES93en option of the tail command, it is easy to view the changing log files. tail-f filename displays the last part of filename on the screen and refreshes it so that you can see the latest file contents.

grammar

Standard grammar
tail [ -f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]

To display the rows in reverse order
tail [ -r ] [ -n Number ] [ File ]

describe

The tail command writes the file specified by the File parameter to standard output from the specified point. If no file is specified, standard input is used. The Number variable specifies how many units to write to standard output. The value of the Number variable can be a positive or negative integer. If the value is preceded by a + (plus sign), the file is written to standard output from the number of units specified at the beginning of the file. If the value is preceded by a - (minus sign), the file is written to standard output starting with the number of units specified at the end of the file. If the value is not preceded by a + (plus sign) or a - (minus sign), the file is read from the cell number specified at the end of the file.

The cell type used by the Number variable to determine the starting point of the count is determined by the -ES120en, -ES121en, -ES122en, -ES123en, and -ES124en flags. If no one of these flags is specified, the tail command reads the last 10 lines of the specified file and writes it to standard output. This is the same as typing -ES126en 10 on the command line.

The -ES130en flag provides a result of 1 in both single-byte and double-byte character environments. The -c flag should be used with caution when the input is a text file containing multi-byte characters, as the resulting output may not start at a character boundary.

mark

-b Number

The specified file is read from the position of the 512-byte block represented by the Number variable.

-c Number

The specified file is read from the byte position represented by the Number variable.

-f

If the input file is a regular file or if the File parameter specifies FIFO (first in, first out), the tail command does not terminate after the last specified cell of the input file is copied, but continues to read and copy additional cells from the input file (as these cells become available). If the File parameter is not specified and the standard input is a pipe, the -f flag is ignored. The tail-ES164en command can be used to monitor the growth of files being written by another process.

-k Number

The specified file is read from the location of the 1KB block represented by the Number variable.

-m Number

The specified file is read from the position of the multi-byte character represented by the Number variable. Use this flag to provide a result of 1 in a single-byte and double-byte character codeset environment.

-n Number

The specified file is read from the position of the row represented by the Number variable.

-r

Displays the output in reverse order from the end of the file. - The default value of the r flag is to print the entire file in reverse order. If the file is greater than 20,480 bytes, the -ES198en flag shows only the last 20,480 bytes.

The -r logo is valid only when the -n logo starts from 1. Otherwise, it will be ignored.

Exit status

This command returns the following exit values:

0

Complete successfully.

[

0
An error occurred.

]

The sample

To display the last 10 lines of the notes file, type:

tail notes To specify the number of lines to read from the end of the notes file, type:

tail -n 20 notes

To display notes 1 page at a time, starting at byte 200, type:

tail -c +200 notes | pg

To track file growth, type:

tail -f accounts

This shows the last 10 lines of the accounts file. The tail command continues to display the lines added to the accounts file. The display will continue 1 straight until you press Ctrl-ES260en button order to stop.


Related articles: