Linux Commands

Linux Tail Command with Examples

As the name implies, the tail command is used to display the last values of the content of the file. By default, the last 10 lines are selected. Whereas, we can customize the number of lines by providing the number in the command.


tail [OPTION]... [FILE]

There can be more than one file in the command. Linux provides a platform for the new users to help them in using Linux tail commands. For this purpose, the following appended command is used:

$ tail --help

The interface that appears contains all possible information regarding the tail. Besides, users can also get help from the manual of Linux. It is a complete guide for the usage of the tail command. It also has all the options and functionality provided by the –tail command.

$ man tail

Example 1:

Consider a simple example in which we will apply the tail command on a file having names of cities. Display the content of the file, where city.txt is the name.

$ cat city.txt

Now apply the tail command on this file. This command will fetch the last 10 lines of the record. The 10 number is constant. So if you do not provide a specific number, then the system by default considers it as 10. And if we specify a number, then the output is limited to that number.

$ tail city.txt

You can see the output contains the last 10 cities present in the file.

Example 2:

Now in this example, we will provide a number to get the desired output from the file content. 4 lines are to be selected by applying a command. –n stands for the number, and it is the number of lines we want to print. Num is mandatory to get a specific output. Otherwise, an error is displayed. n can be removed but “-“ is always required.

$ tail –n 4 city.txt

It is visible from the output that the last 4 lines are displayed.

Example 3:

The tail command also has a plus option “+” in which the output is obtained from the specified number that is mentioned in the command till the end of the file.

$ tail +14 city.txt

In this example, we have taken 14 lines to provide output from this line until the last line in the input file.

Example 4:

Now, moving forward, we come to know the usage of –c in the tail command. So, -c provides the output of the bytes of the number provided in the command. It must add –c with the positive or negative number in your command because without this, the system will display an error message. Whereas the numbers can be any of the +ive and –ive.

If the num is positive, “+num”, it will display all the content of the file after ignoring the number bytes from the starting of the content in the file. And if it is a negative number, “-num”, it displays the bytes numbers from the last in the file.

Consider some data from the last of the same file city.txt.

We will apply the command using the following condition:

$ tail c -5 city.txt

From the output, it is clear that the –num displays the output bytes from the last. As we have provided the negative sign with the number it implies, the bytes will be counted from the last. The number is 5, so the bytes will move 5 steps from the end till 5 characters are covered.

Similarly, take another example in which the number -5 is replaced with -31. This will contain 4 lines including each new line as a new character.

$ tail –c -31 city.txt

If we do not provide any sign with the number, then the num of the last bytes is displayed from the input file.

Example 5:

This example deals with the use of –q in the tail command. “-q” implies the join function. It is used when two or more files are to be displayed and taken as input in the tail command. Consider two files city.txt and capitals.txt that are used as input in the command.

Without Adding –q in the Command

If we do not add –q in the command, and only use the tail command with the names of two files, then the output will display both the files’ names will be displayed before displaying the content between the files. We can observe it by executing the below-cited command:

$ Tail city.txt capitals.txt

The output depicts that the content of both files is followed by their file names. And as it is a tail command and we don’t have mentioned any number. So, by default, the last 10 lines will be shown as a result.

Adding –q in the tail Command

Now we will add “-q” with two file names in the same command mentioned above. By using this, file names that were previewed in the last example are not displayed. Only the file content is displayed as an output and is aligned in a sequence.

$ Tail –q city.txt capitals.txt

From the output, you can see that the data of both files is displayed without names as a heading. The First 10 lines are of one file and the second 10 lines are from the 11th line containing the data of the second file.

Example 6:

By using the keyword –v, the data in the file is always displayed with the filename. The tail will provide the last 10 names with the filename.

$ tail –v city.txt

You can see that the values are printed with the file name at the top.

Example 7:

We will use the sort command in the tail command. For this, we need a file having country names in it. We will firstly display the file content using the cat command.

There are two cases that we need to consider. The first is to use the tail command before the pipe and the sort command after the pipe. The second case is vice versa. You will see that in both cases results will be different.

Now consider the first case. We want the last 6 lines as described in the output, so according to the command, first, the last 6 lines will be selected and the sort command will arrange them alphabetically.

$ tail –n 6 country.txt | sort

The second case is to first sort all the data in the file and then select the last 6 elements.

$ sort country.txt | tail –n 6

From both outputs, you can see the difference.

Example 8:

In this example, we will use the tail with the head command.

Consider the file city.txt as the tail is provided with the positive number. Then the lines starting from the 10th index will be printed and last for 5 lines. As head gives the number 5.

$ tail –n +10 city.txt | head –n 5

Example 9:

We can get the output with the line number. As you know that in Linux, the nl term is used to form a number list of the content. So we will use this term in our tail command.

$ nl sample1.txt | tail -5

Firstly, the numbers will be assigned, and secondly, the last 5 lines will be selected.


In this article, we have explained the basic functionality of tail command separately, as well as other commands.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.