10 Quick Linux Tail Command with Examples

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.

$ tail asian_countries.txt

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.

$ tail -n 5 asian_countries.txt

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

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
$ tail -n 5 asian_countries.txt europe_countries.txt
$ 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.

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

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
$ 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
$ cat asian_countries.txt | tail -n 5 | sort

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
$ 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
$ man tail

10) Check the tail command version

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

$ tail --version



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store