Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of tail command in linux

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "the usage of tail command in linux". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Command format

Tail [necessary parameters] [Select parameters] [File]

2. Command function:

Used to display the end of the specified file, and when no file is specified, it is processed as input information. Commonly used to view log files.

3. Command parameters:

-f loop read

-Q does not display processing information

-v displays detailed processing information

Number of bytes displayed by-c

-n shows the number of rows

-- pid=PID is used with-f to indicate that it ends after the process ID,PID dies.

-Q,-- quiet,-- silent never outputs the first part of the given file name

-s,-- sleep-interval=S is used with-f to indicate dormancy for S seconds at intervals of each iteration.

4. Use an example:

Example 1: displays the content at the end of the file

Command:

Tail-n 5 log2014.log

Output:

The code is as follows:

[root@localhost test] # tail-n 5 log2014.log

2014-09

2014-10

2014-11

2014-12

= = [root@localhost test] #

Description:

Show the last five lines of the file

Example 2: loop through the contents of the file

Command:

Tail-f test.log

Output:

The code is as follows:

[root@localhost ~] # ping 192.168.120.204 > test.log &

[1] 11891 [root@localhost ~] # tail-f test.log

PING 192.168.120.204 (192.168.120.204) 56 (84) bytes of data.

64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms

64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms

64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms

64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms

64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms

64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms

64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms

64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms

64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms

[root@localhost ~] #

Description:

Ping 192.168.120.204 > test.log & / / ping the remote host in the background. The practice of outputting files to test.log; is also used for more than one file monitoring. Terminate with Ctrl+c.

Example 3: display the file from line 5

Command:

Tail-n + 5 log2014.log

Output:

The code is as follows:

[root@localhost test] # cat log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

= =

[root@localhost test] # tail-n + 5 log2014.log

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

= =

Add:

Use

Writes the file to standard output from the specified point. Using the-f option of the tail command, you can easily view the changing log file. Tail-f filename will display the last content of the filename on the screen and not only refresh 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 rows in reverse order

Tail [- r] [- n Number] [File]

Description

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 cells are written 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 starting with the number of cells 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 cells specified at the end of the file. If the value is not preceded by a + (plus sign) or-(minus sign), the file is read from the unit number specified at the end of the file.

The type of unit used by the Number variable to determine the starting point of the count is determined by the-b,-c,-k,-m, and-n flags. If no one of these flags is specified, the tail command reads the last ten lines of the specified file and writes it to standard output. This is the same as typing-n 10 on the command line.

The-m flag provides consistent results in single-byte and double-byte character environments. The-c flag should be used with caution when the input is a text file that contains multi-byte characters, as the resulting output may not start at the character boundary.

Mark

-b Number

Reads the specified file from the 512-byte block location represented by the Number variable.

-c Number

Reads the specified file 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, the tail command does not terminate after copying the last specified unit of the input file, but continues to read and copy additional units from the input file (when those units are available). If no File parameter is specified and the standard input is a pipe, the-f flag is ignored. The tail-f command can be used to monitor the growth of files being written by another process.

-k Number

Reads the specified file from the location of the 1KB block represented by the Number variable.

-m Number

Reads the specified file starting from the multibyte character position represented by the Number variable. Use this flag to provide consistent results in single-byte and double-byte character code set environments.

-n Number

Reads the specified file starting at the line location 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 larger than 20480 bytes, the-r flag displays only the last 20480 bytes.

The-r flag is valid only with the-n flag. Otherwise, it will be ignored.

Exit statu

The command returns the following exit values:

0

Completed successfully.

> 0

An error has occurred.

Example

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

Tail notes to specify the number of lines to read from the end of the notes file, enter:

Tail-n 20 notes

To start with byte 200, display the notes file one page at a time, enter:

Tail-c + 200notes | pg

To track the growth of the file, enter:

Tail-f accounts

This displays the last ten lines of the accounts file. The tail command continues to display the lines added to the accounts file. The display continues until you press the Ctrl-C key order to stop.

File

/ usr/bin/tail

Contains the tail command.

This is the end of the introduction to "the use of tail commands in linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report