tail


The tail command is the complement of the head command, and filters a stream so that only the end of the stream is passed to stdout. This is often used to get a quick summary of the contents of a file, or to target specific lines by index in a pipeline.

The call signature is:

tail {options} {path}

If path is specified, that file will be read and streamed to stdin. If path is omitted or set to -, then this command will operate on the stream that is piped or redirected to stdin.

The most common option is -n (or the --lines long option), which defines the index of the line from which content is passed to stdout. By default the index is 10.

To demonstrate, let's work with our numbers.txt file, which contains a simple list of numbers from one through twelve.:

ninja$: tail numbers.txt
three
four
five
six
seven
eight
nine
ten
eleven
twelve
ninja$:  

Although this file contains 12 lines of text, only the last 10 lines are passed to stdout,

Now lets change the index to 3:

·
·
·
·
·
ninja$: tail -n 3 numbers.txt
ten
eleven
twelve
ninja$:  

The tail command also allows the index to be negative, so that lines can be filtered relative to the beginning of the file. For example, to filter out only the first 5 lines the index can be set to -5:

·
·
·
ninja$: tail -n -5 numbers.txt
eight
nine
ten
eleven
twelve
ninja$: