The tail Command

The tail command reads the final few lines of any text given to it as an input and writes them to standard output (which, by default, is the monitor screen).

The basic syntax for tail is:

tail [options] [filenames]

The square brackets indicate that the enclosed items are optional. By default, tail returns the final ten lines of each file name that is provided to it.

For example, the following command will print (traditional Unix terminology for write) the last ten lines of the file named aardvark in the current directory (i.e., the director in which the user is currently working) to the display screen:

tail aardvark

If more than one input file is provided, tail will print the last ten lines from each file to the monitor screen. Each set of lines will be preceded by the name of the file and separated by one vertical space from other sets of lines. The following is an example of using tail with multiple input files:

tail aardvark anteater armadillo

If it is desired to print some number of lines other than the default ten, the -n option can be used followed by an integer indicating the number of lines desired. For example, to print the final 15 lines from each file in the above example, the command would be modified as follows:

tail -n15 aardvark anteater armadillo

-n is a very tolerant option. For example, it is not necessary for the integer to directly follow it without a space in between. Thus, the following command would produce the same result:

tail -n   15 aardvark anteater armadillo

In fact, the letter n does not even need to be used at all. Just the hyphen and the integer (with no intervening space) are sufficient to tell tail how many lines to print. Thus, the following would produce the same result as the above commands:

tail -15 aardvark anteater armadillo

tail can also print any desired number of bytes (i.e., a sequence of eight bits and usually long enough to represent a single character) from the end of each file rather than a desired number of lines. This is accomplished using the -c option followed by the number of bytes desired. For example, to view the final five bytes of each of the two files aardvark and anteater, the following command would be used:

tail -c 5 aardvark anteater

When tail counts by bytes, it also includes the newline character, which is a non-printing (i.e, invisible) character that is designated by a backward slash and the letter n (i.e., \n). Thus, for example, if there are three new, blank lines at the end of a file, they will be counted as three characters, along with the printing characters (i.e., characters that are visible on the monitor screen or paper).

The number of bytes or lines can be followed by a multiplier suffix. That is, adding the letter b directly after the number of bytes multiplies it by 512, k multiplies it by 1024 and m multiplies it by 1048576. Thus, the following command would print the last five kilobytes of the file aardvark:

tail -c5k aardvark

The -c option is less tolerant than the -n option. That is, there is no default number of bytes, and thus some integer must be supplied. Also, the letter c cannot be omitted as can the letter n, because in such case tail would interpret the hyphen and integer combination as the -n option. Thus, for example, the following would produce an error message something like tail: aardvark: invalid number of bytes:

tail -c aardvark

If tail is used without any options or arguments (i.e., inputs), it will await input from the keyboard and will successively repeat (i.e., each line will appear twice) on the monitor screen each of the final ten lines typed on the keyboard. If it were desired to repeat some number of lines other than the default ten, then the -n option would be used followed by the integer representing that number of lines (although, again, it is not necessary to include the letter n), e.g.,

tail -n3

As is the case with other command line (i.e., all-text mode) programs in Unix-like operating systems, the output of tail can be redirected from the monitor to a file or printer using the redirection operator (which is represented by a rightward pointing angular bracket). For example, the following would write the final 12 lines of the file Yuriko to the file December:

tail -n 12 Yuriko > December

If the file named December did not yet exist, the redirection operator would create it; if it already existed, the redirection operator would overwrite it. To avoid erasing data on an existing file, the append operator (which is represented by two rightward pointing angular brackets) could be used to add the output from tail to the end of a file with that name if it already existed (or otherwise create a new file with that name), i.e.,

tail -n 12 Yuriko >> December

The output from other commands can be piped (i.e., sent) to tail to use as its input. For example, the following sends the output from the ls command (which by default lists the names of the files and directories in the current directory) to tail, which, in turn, prints the final ten lines of the output that it receives from ls to the monitor screen:

ls | tail

This output could easily be redirected, for example to a file named last_filenames as follows:

ls | tail >> last_filenames

It could also be piped to one or more filters for additional processing. For example, the sort filter could be used with its -r option to sort the output in reverse alphabetic order prior to writing to a file:

ls | tail | sort -r >> last_filenames

The -q (i.e., quiet) option causes tail to not print the file name before each set of lines and to eliminate the vertical space between each set of lines when there are multiple input sources. The -v (i.e., verbose) option causes tail to print the file name even if there is just a single input file.

Tail could be viewed as a counterpart of the head command, which always starts reading from the beginning of files and which can continue until any specified distance from the beginning. However, there are a few differences. Perhaps the most useful of these is that tail is somewhat more flexible in that, in addition to being able to start reading any specified distance from the end of a file, it can also start at any specified distance from the beginning of a file.

Tail can be instructed to begin printing from some number of lines or bytes from the start of a file by preceding the number with a plus sign instead of a minus sign. For example, the following would print each of the designated files to the display monitor beginning with the seventh line and until the end:

tail +7 aardvark anteater armadillo

The c option could be used to tell tail to print each of the designated files beginning with the seventh byte instead of the seventh line:

tail +7c aardvark anteater armadillo

A particularly common application for tail is examining the most recent entries in log files. This is because the newest entries are appended to the ends of such files, which tail excels in showing. As log files can be a rather long, this can eliminate a lot of scrolling that would be necessary if some other command were used to read them. For example, the most recent entries to the log /var/log/messages can easily be viewed by using the following:

tail /var/log/messages

As is the case with other programs on Unix-like operating systems, additional information, including variations on specific versions, can be obtained about the tail and head commands by consulting the built-in manual and information pages using commands such as:

man tail


info head

Created April 9, 2005.
Copyright © 2005 Bellevue Linux. All Rights Reserved.