tail Command
Purpose
Displays the last few lines of a file.
Syntax
Description
The tail command writes the file that is specified by the
File parameter to standard output beginning at a 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 for the Number variable can be a positive
or negative integer. If the value is preceded by + (plus sign), the file is written
to standard output that starts at the specified number of units from the beginning of the file. If
the value is preceded by - (minus sign), the file is written to standard output
that starts at the specified number of units from the end of the file. If the value is not preceded
by + (plus sign) or - (minus sign), the file is read starting at
the specified number of units from the end of the file.
The type of unit used by the Number variable to determine the starting point for the count is determined by the -b, -c, -k, -m, or -n flag. If one of these flags is not specified, the tail command reads the last ten lines of the specified file and writes them to standard output. This option is the same as entering -n 10 at the command line.
The -m flag provides consistent results in both single- and double-byte character environments. The -c flag must be used with caution when the input is a text file that contains multibyte characters because output can be produced that does not start on a character boundary.
Flags
| Item | Description |
|---|---|
| -b Number | Reads the specified file beginning at the 512-byte block location that is indicated by the Number variable. |
| -c Number | Reads the specified file beginning at the byte location that is indicated by the Number variable. |
| -f | If the input file is a regular file or if the File parameter specifies a FIFO (first-in-first-out), the tail command does not terminate after the last specified unit of the input file is copied, but continues to read and copy more units from the input file as they become available. If no File parameter is 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 that is written by another process. |
| -k Number | Reads the specified file beginning at the 1 KB block location that is indicated by the Number variable. |
| -m Number | Reads the specified file beginning at the multibyte character location that is indicated by the Number variable. Using this flag provides consistent results in both single- and double-byte character-code-set environments. |
| -n Number | Reads the specified file from the first or last line location as indicated by the sign
(+ or - or none) of the Number variable and
offset by the number of lines Number. |
| -r | Displays the output from the end of the file in reverse order. The default for the -r flag prints the entire file in reverse order. If the file is larger than 20,480 bytes, the -r flag displays only the last 20,480 bytes. |
Exit status
This command returns the following exit values:
| Item | Description |
|---|---|
| 0 | Successful completion. |
| >0 | An error occurred. |
Examples
- To display the last 10 lines of the
notesfile, enter the following command:tail notes - To specify the number of lines to start reading from the end of the notes
file, enter the following command:
tail -n 20 notes - To display the
notesfile a page at a time, beginning with the 200th byte, enter the following command:tail -c +200 notes | pg - To follow the growth of a file, enter the following command:
tail -f accountsThis command displays the last 10 lines of the
accountsfile. The tail command continues to display lines as they are added to theaccountsfile. The display continues until you press the Ctrl+C key sequence to stop it.
File
| Item | Description |
|---|---|
| /usr/bin/tail | Contains the tail command. |