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.

Syntax:

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-156.png" data-lazy- height="243" src="data:image/svg xml,” width=”701″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-158.png" data-lazy- height="298" src="data:image/svg xml,” width=”713″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-160.png" data-lazy- height="311" src="data:image/svg xml,” width=”510″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-163.png" data-lazy- height="206" src="data:image/svg xml,” width=”367″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-165.png" data-lazy- height="96" src="data:image/svg xml,” width=”428″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-166.png" data-lazy- height="77" src="data:image/svg xml,” width=”438″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-169.png" data-lazy- height="169" src="data:image/svg xml,” width=”346″>

We will apply the command using the following condition:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-170.png" data-lazy- height="48" src="data:image/svg xml,” width=”455″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-172.png" data-lazy- height="96" src="data:image/svg xml,” width=”450″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-174.png" data-lazy- height="437" src="data:image/svg xml,” width=”627″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-176.png" data-lazy- height="385" src="data:image/svg xml,” width=”568″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-178.png" data-lazy- height="224" src="data:image/svg xml,” width=”428″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-180.png" data-lazy- height="202" src="data:image/svg xml,” width=”409″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-181.png" data-lazy- height="133" src="data:image/svg xml,” width=”502″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-182.png" data-lazy- height="131" src="data:image/svg xml,” width=”494″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-183.png" data-lazy- height="112" src="data:image/svg xml,” width=”547″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-184.png" data-lazy- height="116" src="data:image/svg xml,” width=”460″>

Conclusion

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

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg60e8c5554000f.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.