In our previous tutorial, we looked at the Linux head command and its example usages. The tail command is the complementary of the head command. It reads and prints out the last N lines in a file. Without any command options, it prints out the last 10 lines in a text file. In this guide, we will focus on the tail command and explore the various options that come with the command.

The tail command takes the following Syntax:

$ tail [options] files(s) 

1 ) Display the last 10 lines of a file 

As pointed out earlier, the tail command, without any arguments, will display the last 10 lines of a file. We have a sample file called asian_countries.txt – It contains a list of countries in the Asian continent.

To list the last 10 countries in the file, run the command:

$ tail asian_countries.txt

Default-Tail-Command-Output

2) Display the last N lines in a file 

Suppose you want to display a specific number of lines and not the default 10 lines. To achieve this, use the -n flag followed by the number of lines.

For example, to display the last 5 lines. To do so, invoke the command:

$ tail -n 5 asian_countries.txt

Display-n-number-of-lines-with-tail-command

3) Print filename header

To add a header tag that corresponds to the file name, use the -v option as follows:

$ tail -v asian_countries

In the example below, the tag asian_countries is  printed first followed by the last ten lines of the file,

Print-FileName-Header-Tail-Command

4) Display the last n lines from multiple files

Moreover, you can list the last N lines from multiple files using the syntax below:

$ tail -n 5 file_1 file_2

We have another file called europe_countries.txt  that contains a list of European countries. To print the last 5 lines in both asian_countries.txt and europe_countries.txt text files, run the command:

$ tail -n 5 asian_countries.txt europe_countries.txt

This time around, notice the demarcation of the contents of the two files using the filename header that we mentioned in the previous step. When viewing contents from multiple files, the tag names are automatically added for better presentation.

Display-lines-from-multiple-files-tail-command

If you wish to suppress, file name headers in tail command’s output then use ‘-q’ option, example is shown below:

$ tail -q -n 5 asian_countries.txt europe_countries.txt

5) Save the output of tail command to a text file

If you don’t have much time to view the output and would prefer, instead, to view it later on, you can save it on a text file using the greater-than ( > ) redirection operator.

For example, to save the output of the last 5 lines contained in the asian_countries.txt file to the text file output.txt invoke the command.

$ tail -n 5 asian_countries.txt > output.txt

Save-Tail-Command-Output

NOTE: The greater-than sign  ( > )  overwrites the file when used for a subsequent time using the same file. The original content gets overwritten and new content is written.

To append or add output, use the double greater than sign ( >> ). This adds the output to the file instead of overwriting it.

For example, to append the last 5 lines of the europe_countries.txt file to the  output.txt file, run the command:

$ tail -n 5 europe_countries.txt >> output.txt

Using the cat command, you can see that the output file now contains data from both files.

Append-Tail-Command-Output

6 ) Use the -f option to monitor real-time log files

When used with the -f option, the tail command is mostly used by sysadmins to monitor log files in real-time. For example, to monitor the Syslog log file in real-time, run the command below. You will notice some log output at the bottom of the terminal every few seconds.

$ sudo tail -f /var/log/syslog

Real-Time-Logs-Monitoring-Tail-Command

Use ‘-F’ option in tail command when you want to keep monitoring the log file even at its rotation. When file gets rotated  then tail command will automatically start printing lines from new file if we are using ‘-F’ option.

$ sudo tailf -F /var/log/syslog

7) Using tail command with pipes

In Linux, pipes provide a cool way of processing text. With tail command, you can pipe the output and further modify what will be displayed. For instance, you can sort the output alphabetically as follows:

$ tail asian_countries.txt | sort

Tail-Command-Output-Sort

You can also use multiple pipe statements for example:

$ cat asian_countries.txt | tail -n 5 | sort

So, what does the above command do? First, the cat command prints out the contents of the asian_countries.txt text file. The information is piped to the tail command that prints out the last five lines. Then finally, the information is sorted in alphabetical order by the sort command.

8) Print N number of bytes data from a file

Using ‘-c’ option in tail command, we can print n number of bytes data from a file.

$ tail -c 400 /var/log/kern.log

Above tail command will display 400 bytes of data from the bottom of the file.

We can also print the data in KB and MB using ‘K’ and ‘M’ parameters, example is shown below

$ tail -c 4k /var/log/kern.log
$ tail -c 4M /var/log/kern.log

9 ) Get help with tail command options

To get help with more tail command options run the command:

$ tail --help

Tail-help-Command

Alternatively, you can visit the man pages as shown:

$ man tail

man-tail-command

10) Check the tail command version

Lastly, to check the version of the tail command, execute:

$ tail --version

Check-Tail-Command-Version

That’s all from this guide. I hope you find it informative. Please do share your feedback and comments.

Read Also : 8 Stat Command Examples in Linux

The post 10 Quick Linux Tail Command with Examples first appeared on LinuxTechi.

Leave a Comment