Display the last lines of a file in Unix

Use the Unix command tail to read from standard input or a file and send the result to standard output (that is, your terminal screen). The format for using the tail command is:

 tail [ +-[number][lbcr] ] [file]

Everything in brackets is an optional argument. If you don't specify a filename, tail uses standard input.

Tail begins at distance +number from the beginning or -number from the end of the input. The number is counted in units of lines, blocks, or characters, according to the appended options -l, -b, or -c. When you don't specify a unit, tail operates based on lines.

Specifying -r causes tail to print lines from the end of the file in reverse order. The default for -r is to print the entire file this way. Specifying -f causes tail not to quit at the end of the file, but rather to reread the file repeatedly (useful for watching a "growing" file such as a log file).

For example, given a file containing the English alphabet with each letter on a separate line, the command:

 tail -3 alphabetfile

would report:

 x y z

You can use tail with pipes. For example, to see the sizes of the last few files in the current directory, you could enter at the Unix prompt:

 ls -l | tail

To save this information in a file in the current directory named mylist, at the Unix prompt, enter:

 ls -l | tail > mylist

For more detailed information on tail, consult the Unix manual page by entering at the prompt:

 man tail

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document acrj in the Knowledge Base.
Last modified on 2019-06-18 14:46:57.

Contact us

For help or to comment, email the UITS Support Center.