Skip to main content

File Viewing Commands

tail Command Examples

Learn the tail command syntax along with some practical examples of the tail command in Linux. This will also help you in monitoring the log files.

Cat command is one of the most used commands for displaying the contents of a file in Linux. But you might not always want to display all the lines of a big file. Tail command helps in such a scenario.

What is the Tail Command?

The tail command, as the name suggests, outputs the last parts of a single file or multiple files. By default, the tail command prints the last ten lines of the input files. The tail command is also used for reading log files in real time.

The syntax for the tail command is:

tail [options] [files]

5 practical examples of Tail command in Linux

Let’s learn how to use the tail command in Linux with some practical examples. I’ll use this text file in this tutorial:

The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

You can download the sample file from our GitHub repository and follow the tutorial while practicing it at the same time.

If you use the tail command without any option, it will print the last 10 lines. That’s the default behavior of the tail command.

tail agatha.txt
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

If the file has less than ten lines, it will display only the available lines. If the last few lines are blank, they will still be counted as valid lines.

But you don’t have to content yourself with the default usage of the tail command. There is more to it. Let’s see the most common usage of tail commands one by one.

Linux Tail Command Examples

1. Print last N lines with tail command

To view the last N lines, instead of the default 10, you can use the tail command in the following manner:

tail -n N <filename>

For example, if you want to see the last 5 lines with tail command in our example file, you can use it like this:

tail -n 5 agatha.txt
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

Tip: You can also simply use tail -N instead of tail -n N to display last N lines of a file.

2. Print all lines starting with the line number N

If you want to view all the lines starting from line number N, you can use the + option here.

tail -n +N <filename>

In our sample file if you want to see all the lines starting from line number 7, you can use it in this manner:

tail -n +7 agatha.txt
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

3. Using multiple files with tail command

The tail command allows you to use more than one files at the same time. All the standard tail command options are applicable to the multiple files.

tail -n N <file1> <file2> <file3>

The output is combined for all the files and by default, the file name is displayed in lines beginning with “==>”.

For example, if you want to see the last three lines of the files sherlock.txt and agatha.txt, it would be like this:

tail -n3 sherlock.txt agatha.txt
==> sherlock.txt <==
The Adventure of the Noble Bachelor
The Adventure of the Beryl Coronet
The Adventure of the Copper Beeches

==> agatha.txt <==
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

Tip: You can remove the file names from the output by using the quiet mode with option -q.

4. Monitor files in real time with tail command [Very useful for log monitoring]

Suppose you have a file and new content is added to it. The tail command allows you to display all the new lines as they are added to the file.

For this, you can use the -f option.

tail -f <log-file>

The command will first display the last 10 lines of the files and then it will update the output as the new lines are added to the file.

This is widely used for watching log files in real time. This is perhaps the most practical use of the tail command.

Tip: If you use -F instead of -f option, the tail command will wait for the input file to be created (if it doesn’t exist already) and then display the content of the file in real time.

5. Using the tail command with pipes

The tail command can be used in conjugation with other commands using pipes.

For example, if you have too many files in a directory and you only want to see the last 3 modified files, you can use it in the following fashion:

ls -ltr | tail -n3

In the above command, ls -lrt lists all the files in reverse chronological order. And then the tail command further parses this output and displays only the last three lines of the ls command output and thus we get the list of the last three modified files.

Bonus Tip: Show the tail command output with line numbers

Line numbers help a lot in comprehending and analyzing the output. Suppose you displayed the last 20 lines of a file but you also want to see their line numbers so that you can see the total lines in the file.

Unfortunately, there is no built-in option to display the tail command output with numbers.

But that doesn’t mean you cannot display the line numbers with the tail command. For this purpose, you can use the power of pipes. You just saw in the previous section that tail commands can be used with pipes. Why not use it here.

nl is the way to display the content of a file with line numbers. If you combine it with the tail command using pipe, you get to display the output of tail command with line numbers.

nl <filename> | tail -3

I hope you find the tail command examples helpful. I recommend reading about head command as well. If you have any suggestions or questions, please share it in the comment section below.