how to use the tail command to view files in linux

There are several different ways to view text files in Linux. You can use a text editor such as vi or gedit. You can also use an utility like more or less that will print out the complete content of the file. tail is yet another utility that will allow you to view the contents of a file.

However, there is one feature in the tail utility that makes it extremely useful… can stream file content using tail. This makes it extremely useful while viewing files that are constantly changing or are updating as you are viewing it. One practical use of the streaming functionality is to view log files of applications that are running.

It is also useful when viewing large files. The tail utility has the ability to display just part of the file, specifically the last part of the file or the tail of the file. By default, it displays the last 10 lines of the file.

As with most Linux commands and utilities, the tail command comes with several different command line options. We will just go over a few of the most commonly used ones.

display last n lines

The command line option that controls the output is -n or –lines. If you do not specify any, then it will default to 10 and display the last 10 lines. The number after the -n option specifies the number of the lines to be displayed from the end of the file. So, the example below will display the last 75 lines of the file.

bash$ tail -n 75 /path/to/file

You can also specify the line count from the top of the file, by using the plus (+) symbol in front of the number. To display all lines except the first 26 lines, you can use the command:

bash$ tail -n +26 /path/to/file

stream file after displaying last n lines

When you are viewing files that are constantly updating, such as a log file then you want to stream the contents rather than just display it. You can use -f or –follow command line option to do so. You may do it along with the -n option mentioned above.

bash$ tail -n 45 -f /path/to/file

Many applications does rollover of log files when log files reach a certain size. So what essentially happens here is that the file is copied to a new location and a new file with the same name created. If you have been tailing the file, then the tail utility loses the handle to the file during this process and will stop working. To counter this issue, there is a retry (–retry) option that keep trying to open the file when it becomes inaccessible.

bash$ tail -n 45 -f --retry /path/to/file

You can use the -F command line option which is the same as –follow –retry.

bash$ tail -F /path/to/file

stream file with piping

Just as with almost all Linux commands that outputs to the standard out stream, you can pipe the tail command output as well. If you want to see only error messages in a log file that you are streaming, you can pipe the tail output through a grep.

bash$ tail -F /path/to/logfile | grep -i "error"

There is a companion command utility named head that allows you to view the top portion of a file, just as tail allows you to view the bottom section of the file content.