tail Command in Linux | Explained

The “tail” command in Linux is a powerful tool for viewing the last few lines of a file or input stream. It is often used to view log files, monitor the output of a running process, or debug scripts and programs. This can help you resolve and troubleshoot any issue you are facing regarding a system or application activity.

Let’s start with the basic syntax/working of the tail command.

How Does the tail Command Work in Linux?

To display the content of any file on the terminal, we make use of the “cat” command which is used as follows:

$ cat demo

As you can see in the above image that the above file contains 15 different entries in each line, followed by the line breaks. So, when you read the same file followed by the tail command then, you will get the last 10 entries as an output shown below:

$ tail demo

The tail command comes with different flags to alter the output of any file, and you can get that information by typing the command mentioned below:

$ tail --help

Some of the most common options associated with the tail command will be discussed in the next section

How to Use the tail Command?

As previously discussed, the tail command comes with different options, and some of them are discussed below:

Display Only Specific Number of Lines using the tail Command

You can use this command in Linux to display the last part of a text file, with the “-n” option specifying the number of lines to display.

For example, if you want to view the last 5 lines of a ‘demo’ file, then you can type the following command:

$ demo -n 5 demo

You can also use the same command to read the content of multiple files by following the command mentioned below:

$ tail -n 5 demo demo2

Display the Content of a File by Specifying the Characters (Bytes)

You can use the tail command to display only specific characters and bytes of any text file using the “-c” option as shown below:

$ tail -c -50 demo

In the above command, it will display the last 50 characters (bytes) from the text. If you want to display only those characters that start from the 50th character, then you need to type the following command:

$ tail -c +50 demo

Display the Content of the File along with the File Name

The “-v” option is used to provide you with the additional information which is the name of the file in this case as shown below:

$ tail -v demo

Combine a Command with the tail using the pipe Operator

You can also combine the tail command with any other command using the pipe operator (|) as shown below:

$ history | tail

The ‘history’ command shown above is used to keep track of all the commands that we have executed in the terminal. So, in this case, it will display the last 10 executed commands on the terminal.

The same thing could be applied with the cat command, and one of its examples is mentioned below:

$ cat demo | tail

This is how we can use the tail command.

Conclusion

The tail command is a powerful tool in Linux that allows you to view the last part of a file. It is often used to monitor log files for changes or errors, as it allows you to see the most recent entries at the bottom of the file. In this article, we have discussed its functionality and different associated options.