8.4.33 tail command (displays the last part of files)
Syntax
tail [-r] [-b num-blocks|-c num-bytes|-n num-lines|-num-lines] [path-name ...]
Description
This command outputs the last part of one or more files to the standard output. If no file is specified, the standard input is read as the input. The displayed portion of a file begins at a position that is expressed in units of bytes, lines, or blocks. All data found in the specified display range is displayed. No error occurs if there is no data within the specified range.
Arguments
A numeric value prefixed with the plus sign (+) indicates a position from the beginning of the input. For example, -c +2 starts displaying from the second byte from the beginning of the input.
A numeric value prefixed with a minus sign (-) or without a sign indicates a position from the end. For example, -n 2 indicates the second line from the end of the input. The default is -n 10, or 10 lines from the end of the input.
- -r
Specifies that the display is to be line-by-line in reverse order.
When the -r option is specified together with the -b option, the display is line-by-line from the end of the file for the number of blocks specified in the -b option. If display starts from a position that is in the middle of a multibyte character, garbled characters might result.
When the -r option is specified together with the -c option, the display is line-by-line from the end of the file for the number of bytes specified in the -c option. If display starts from a position that is in the middle of a multibyte character, garbled characters might result.
When the -r option is specified together with the -n option or with num-lines specified, the display is line-by-line from the end of the file for the number of lines specified in num-lines or in the -n option.
When the -r option is specified by itself, all input lines are output line-by-line in reverse order from the end of the file. Specifying this option more than once does not result in an error.
- -b num-blocks
Unless the -r option is specified, specifies the position from which to start displaying, in units of blocks (one block is 512 bytes).
A numeric value prefixed with a minus sign (-) or a numeric value without a sign indicates a position counted from the end of the input. A numeric value prefixed with the plus sign (+) indicates a position counted from the beginning of the input.
If num-blocks is omitted, the command displays usage information and terminates with an error message (tail: option requires an argument - option). If you specify a non-numeric value for num-blocks, an error message (tail: illegal offset -- specified-character-string) is output.
If display starts from a position that is in the middle of a multibyte character, garbled characters might result. End-of-line codes are included in the byte count. For example, in Windows, a linefeed [LF] counts as one byte, and [CR] + [LF] counts as two bytes. If this option is specified more than once, usage information is displayed.
- -c num-bytes
Unless the -r option is specified, specifies the position from which to start displaying, in units of bytes.
A numeric value prefixed with a minus sign (-) or a numeric value without a sign indicates a position counted from the end of the input. A numeric value prefixed with a plus sign (+) indicates a position counted from the beginning of the input.
If num-bytes is omitted, the command displays usage information and terminates with an error message (tail: option requires an argument - option). If you specify a non-numeric value for num-bytes, an error message (tail: illegal offset -- specified-character-string) is output.
If display starts from a position that is in the middle of a multibyte character, garbled characters might result. End-of-line codes are included in the byte count. For example, in Windows, a linefeed [LF] counts as one byte, and [CR] + [LF] counts as two bytes. If this option is specified more than once, usage information is displayed.
- -n num-lines|-num-lines
Unless the -r option is specified, specifies the position from which to start displaying, in units of lines.
A numeric value prefixed with a minus sign (-) or a numeric value without a sign indicates a position counted from the end of the input. A numeric value prefixed with a plus sign (+) indicates a position counted from the beginning of the input.
If num-lines is omitted, the command displays usage information and terminates with an error message (tail: option requires an argument - option). If you specify a non-numeric value for num-lines, an error message (tail: illegal offset -- specified-character-string) is output. If this option is specified more than once, usage information is displayed.
- path-name
Specifies an input file. If no input file is specified, the standard input is read. Multiple input files can be specified. If you specify more than one file, each file is identified at the beginning of the output from that file by a blank line (linefeed) and its file name in a header string in the following format:
==> file-name <==
When you execute the tail command with multiple files specified, all the files are processed. If any file fails to open, the command terminates with a return code of 1.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 or greater |
Error termination |
Notes
All data found in the specified display range is displayed. No error occurs if there is no data in the specified range.
If no options are specified, -n 10 is assumed.
In Windows, input and output are performed in the binary mode for files and for the standard input and standard output. No conversion of end-of-line codes is performed.
Examples
Display the last two lines of the files test1.txt and test2.txt.
$ cat test1.txt 0001:test1.txt 0002:test1.txt 0003:test1.txt 0004:test1.txt 0005:test1.txt 0006:test1.txt 0007:test1.txt 0008:test1.txt 0009:test1.txt 0010:test1.txt $ cat test2.txt 0001:test2.txt 0002:test2.txt 0003:test2.txt 0004:test2.txt 0005:test2.txt 0006:test2.txt 0007:test2.txt 0008:test2.txt 0009:test2.txt 0010:test2.txt $ tail -n2 test1.txt test2.txt ==> test1.txt <== 0009:test1.txt 0010:test1.txt ==> test2.txt <== 0009:test2.txt 0010:test2.txt $
Display the fifth and subsequent lines from the beginning of the file test1.txt.
$ cat test1.txt 0001:test1.txt 0002:test1.txt 0003:test1.txt 0004:test1.txt 0005:test1.txt 0006:test1.txt 0007:test1.txt 0008:test1.txt 0009:test1.txt 0010:test1.txt $ tail -n+5 test1.txt 0005:test1.txt 0006:test1.txt 0007:test1.txt 0008:test1.txt 0009:test1.txt 0010:test1.txt $
The following is an example of specifying the -r option.
$ cat zztt1.txt 1:0001:zzzz: 2:0001:aaaa: 3:0001:JJJJ: 4:0001:cccc: 5:0001:cccc: 6:0001:cccc: 7:0001:cccc: 8:0001:cccc: 9:0001:cccc: 10:0001:cccc: 11:0001:cccc: 12:0001:cccc: $ tail -r -n 2 zztt1.txt 12:0001:cccc: 11:0001:cccc: $ tail -r zztt1.txt (display all lines rather than just 10) 12:0001:cccc: 11:0001:cccc: 10:0001:cccc: 9:0001:cccc: 8:0001:cccc: 7:0001:cccc: 6:0001:cccc: 5:0001:cccc: 4:0001:cccc: 3:0001:JJJJ: 2:0001:aaaa: 1:0001:zzzz: $
Below are two more examples that illustrate the -r option.
$ cat block.txt ---> 101 lines of 100 bytes + end-of-line code (\n) 0000000000:1234567890123(omitted)78901234567890123456789012345678T 00001xxx00:1234567890123(omitted)78901234567890123456789012345678T 00002xxx00:1234567890123(omitted)78901234567890123456789012345678T (omitted) 00098xxx00:1234567890123(omitted)78901234567890123456789012345678T 00099xxx00:1234567890123(omitted)78901234567890123456789012345678T 00100xxx00:1234567890123(omitted)78901234567890123456789012345678T $ tail -b 1 block.txt 45678T 00096xxx00:1234567890123(omitted)78901234567890123456789012345678T 00097xxx00:1234567890123(omitted)78901234567890123456789012345678T 00098xxx00:1234567890123(omitted)78901234567890123456789012345678T 00099xxx00:1234567890123(omitted)78901234567890123456789012345678T 00100xxx00:1234567890123(omitted)78901234567890123456789012345678T $ tail -rb 1 block.txt 00100xxx00:1234567890123(omitted)78901234567890123456789012345678T 00099xxx00:1234567890123(omitted)78901234567890123456789012345678T 00098xxx00:1234567890123(omitted)78901234567890123456789012345678T 00097xxx00:1234567890123(omitted)78901234567890123456789012345678T 00096xxx00:1234567890123(omitted)78901234567890123456789012345678T 45678T $ tail -c 110 block.txt 2345678T 00100xxx00:1234567890123(omitted)78901234567890123456789012345678T $ tail -rc 110 block.txt 00100xxx00:1234567890123(omitted)78901234567890123456789012345678T 2345678T $
Display an option error message.
This message might vary depending on the platform in which a command is executed. The following shows an example of Windows.
C:\TEMP>%ADSH_OSCMD_DIR%\tail -z tail: illegal option -- z usage: tail [-r] [-b number | -c number | -n number | -number] [file ...]