Detailed explanation of Linux tail command

Linux command: show end of file

  Head/Tail

  Head and tail are as easy to understand as their names, it is used to display a certain number of text blocks at the beginning or end, head is used to display the beginning of the file to the standard output, and tail is of course the end of the file. , see the following example:

(1)?

  displays the first 6 lines of a file

  head -6 readme.txt

  (2) displays the last 25 lines of a file

  tail -25 mail.txt

  The first example is to display the first 6 lines of the file, and the second example is to display the last 25 lines of the file.

  The following range, which combines the head and tail commands, displays lines 11 to 20 of the file:

(3)?

  head -20 file | tail -10

   The manual page of tail shows more available parameters than head, and there is a very useful parameter " -f ". When this parameter is used, tail will not return the end signal unless we go to interrupt it by ourselves. ; instead, it waits until it finds that the data has been added to a new line since it was last read:

(4)?

  display ongoing updates to the given

  log file

  tail -f /usr/tmp/logs/daemon_log.txt

  The above example can dynamically display the dynamic update of the log file.

  Assuming that the service program has been adding dynamic data to the log file of /usr/adm/logs/daemon_log.txt  , use tail -f in the command line control window, it will track the file in real time for a certain period of time. All updates. (The -f can only be used if its input is a file).

  If you have multiple file parameters after tail, you can track several log files at once in the same window:

  track the mail log and the server error log

  at the same time.

  tail -f /var/log/mail.log /var/log/apache/error_log

  tac -- concatenate in reverse?!

   How to spell cat in reverse? That's right!! This is the function of tac~ It is used to concatenate the order content of the file in reverse, so~ under what circumstances is it used? Any order that requires last in, first out It's useful for rearranging components! For example, the following command lists the three most recently created user accounts, in order from last created to first:

(5)?

  last 3 /etc/passwd records - in reverse

  $ tail -3 /etc/passwd | tac

  curly:x:1003:100:3rd Stooge:/homes/curly:/bin/ksh

  larry:x:1002:100:2nd Stooge:/homes/larry:/bin/ksh

  moe:x:1001:100:1st Stooge:/homes/moe:/bin/ksh

 

A special method for viewing file content
  I believe you are already familiar with the most basic cat and less, more. If you have special requirements:
1. If you only want to see the first 5 lines of a file, you can use the head command, such as:
head - 5 /etc/passwd
2. If you want to view the last 10 lines of the file, you can use the tail command, such as:
tail -10 /etc/passwd
tail -f /var/log/messages
The parameter -f makes tail keep reading The latest content, which has the effect of real-time monitoring

1. TAIL command under UNIX

Note: Part of the content is excerpted from "Forever UNIX", http://fanqiang.chinaunix.net/

NAME

tail - the end of the output file

SYNOPSIS (overview)

../src/tail [OPTION]... [FILE]...

DESCRIPTION (description)

Display the last 10 lines of each FILE on standard output. If there are more than one FILE, they are displayed one after the other, giving the filename at the beginning of each file display. If there is no FILE, or FILE is -, then Read from standard input.

--retry
Keep trying to open the file even if tail is inaccessible at the start or after tail runs. --Only useful with -f .
-c , --bytes = N
output last N bytes
-f , --follow[= {name|descriptor}]
When the file grows, output subsequent data; -f , --follow and --follow = descriptor all mean the same thing
-n , --lines = N
Print the last N lines instead of the default last 10 lines
--max-unchanged-stats = N
See texinfo documentation (default is 5)
--max-consecutive-size-changes = N
See texinfo documentation (default is 200)
--pid = PID
Combined with -f , it means that it will end after the process ID and PID die.
-q , --quiet , --silent
Never output headers that give filenames
-s , --sleep-interval = S
Combined with -f , it means to sleep for S seconds at the interval of each repetition
-v , --verbose
always output the header of the given filename
--help
Exit after displaying help information
--version
Exit after outputting version information

If the first character of N (number of bytes or lines) is `+', then display the Nth item from the beginning of each file, otherwise, display the last N items of the file. N can have a multiple Prefix: b for 512, k for 1024, m for 1048576 (1 megabyte). The first option -VALUE or +VALUE is treated as -n VALUE or -n +VALUE, unless VALUE contains one of the multiples of the [bkm] suffix , in this case it is seen as -c VALUE or -c +VALUE

When using --follow (-f), tail is followed by the file descriptor by default, which means that even if the file displayed by tail is renamed, tail can still trace to the end of the file. If you really want to query the actual name of the file instead of the file descriptor (e.g. log rotation), this default behavior is not what you expect. In this case --follow = name should be used . This will cause tail to periodically reopen the specified file by to track whether it was deleted, or recreated by another program.

        The complete documentation for tail is maintained as the Texinfo manual. If both the info and tail programs are installed on your site, the command: info tail should give you access to the entire manual.

       As you can see from the instructions above, the most common form is: tail -f sys.log

2. TAIL tool under WINDOWS

  There is a good tail tool on sourceforge, I recommend it to everyone
    1. First of all, it is the tail function, which can tail multiple files, such as access.log, error.log
    2. Keyword highlighting function.
    3. Workspace management.
    4. Green , no installation, only 164k

     Download addresshttp: //sourceforge.net/projects/tailforwin32

other:

use

Writes the file to standard output starting at the specified point. Use the -f option of the tail command to easily view the log file that is being changed. tail -f filename will display the last content of filename on the screen, and not only refresh, but also make you see the latest file content.

 

 

grammar

standard syntax

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

 

 

To display rows in reverse order

tail [  -r ] [  -n Number ] [ File ]

 

 

describe

The tail command writes the file specified by the File parameter to standard output starting at the specified point. If no file is specified, standard input is used. The Number variable specifies how many cells 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), writes the file to standard output starting at 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 at the specified number of units at the end of the file. If the value is not preceded by a + (plus sign) or - (minus sign), the file is read starting at the specified unit number at the end of the file.

 

 

The type of cell that the Number variable uses to determine the starting point for counting is determined by the -b, -c, -k, -m, and -n flags. If none of these flags are specified, the tail command reads the last ten lines of the specified file and writes them to standard output. This is the same as entering -n 10 at 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 containing multibyte characters, as the resulting output may not start at a character boundary.

 

 

logo

 

-b Number

Reads the specified file starting at the 512-byte block position represented by the Number variable.

 

-c Number

Reads the specified file starting at 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 copying the last specified cell of the input file, but continues reading and copying additional cells from the input file ( when these units are available). If the File parameter is not specified, and standard input is a pipe, the -f flag is ignored. The tail -f command can be used to monitor the growth of a file being written by another process.

 

-k Number

Reads the specified file starting from the 1KB block position indicated by the Number variable.

 

-m Number

Reads the specified file starting at the multibyte character position represented by the Number variable. Using this flag provides consistent results in both single-byte and double-byte character code set environments.

 

-n Number

Reads the specified file starting at the line position represented by the Number variable.

 

-r

Display 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 20,480 bytes, the -r flag only displays the last 20,480 bytes.

 

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

 

 

exit status

The command returns the following exit values:

 

 

0

Completed successfully.

 

>0

An error occurred.

 

Example

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

 

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

tail  -n 20 notes

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

tail  -c +200 notes | pg

To track file growth, enter:

tail  -f accounts

This shows the last ten lines of the accounts file. The tail command continues to display lines added to the accounts file. The display continues until you stop it by pressing the Ctrl-C key sequence.

document

 

/usr/bin/tail

Contains the tail command.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117572&siteId=291194637