UITS

In Unix, how can I display the last lines of a file?

Use the Unix command tail to read from standard input or a file and send the result to standard output (i.e., 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 At IU, how do I get support for Linux or Unix?

This is document acrj in the Knowledge Base.
Last modified on 2008-10-02.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.