Tail Command in Linux

Learn via video courses
Topics Covered

Overview

The tail command in Linux is a useful utility for viewing the last few lines of a file. It is commonly used for monitoring log files, where it is often necessary to see the most recent entries. The tail command is included with most Linux distributions and is also available on other operating systems, such as macOS and FreeBSD. It is a simple yet powerful command that can be customized with various options and arguments to suit different requirements.

Introduction to Tail Command in Linux

The tail command is a powerful and widely used utility in Linux that allows users to display the last few lines of a file or stream of text. It is a simple and powerful tool that is frequently used for monitoring log files and analyzing system performance. It is similar to the cat command which is used to display an entire text file, although the significant difference is that the tail command will only display by default the last 10 lines of a text file or as specified.

The tail command is highly customizable with options such as specifying the number of lines or bytes to display, setting the time interval between updates, and suppressing or displaying headers when monitoring multiple files. It can also be used to follow files and monitor the output of commands in real time. This makes the tail command an essential tool for system administrators and developers and its ease of use and flexibility make it a valuable asset in any Linux user's toolkit.

Linux Tail Command Syntax

The syntax of the tail command in Linux is as follows:

Here, “options” are the various flags and parameters that can be used to modify the behaviour of the tail command, and “file-name” is the name of the file that is to be monitored.

By default, the tail command displays the last 10 lines of the file. However, you can use the -n option to specify a different number of lines to display.

For example:

This command will display the last 3 lines of the file named file.txt. We are going to learn about some of the common tail options further in this article.

Tail Options

The tail command comes with several options that allow users to customize their behaviour.

Some of the most commonly used options include:

OptionsDescription
-nThe -n option allows users to specify the number of lines to display from the end of the file.
-fThe -f option follows the file and displays new entries in real time as they are added. This is useful for monitoring log files and tracking the progress of long-running processes.
-cThe -c option is used to specify the number of bytes to be displayed from the end of the file.
-qThe -q option is used to suppress the display of headers when multiple files are being monitored.
-vThe -v option is used to display the headers when monitoring multiple files.
-sThe -s option is used to define the time interval between updates when using the -f option. The default value is 1 second.

Applications of Tail Command

The tail command in Linux is a simple but very useful utility.

Some of its applications are listed below -

  • Monitoring Log Files: One of the most common applications of the tail command is monitoring log files in real time. The -f option can be used to follow a log file as new entries are added, making it easy to monitor the activity of a system or application.
  • Debugging: The tail command can be used to help debug applications by monitoring their output. By using the -f option, developers can track the progress of their code as it runs and debug any issues that may arise.
  • Analyzing System Performance: The tail command can be used to monitor system performance by following log files related to system processes. , For example,, the tail command can be used to monitor CPU usage, network traffic, and other system metrics in real time.
  • Viewing Large Files: When working with large files, it can be time-consuming to open the entire file to find a specific piece of information. The tail command can be used to display only the last few entries of a file, making it easier to quickly find the information you need.
  • Concatenating multiple files: The tail command can be used to concatenate multiple files by using the -q option to suppress the file name headers and combine them into a single file. This can be useful when working with log files that are split across multiple files.

Tail Command Examples

To better understand the tail command, its options, and applications, we will go through a few examples, which include some of the most common ways in which the tail command in Linux is used.

Let us consider two text files, laureus-winners.txt and year.txt which contain the names of the last 15 Laureus winners and the years they won it, respectively. Let us see a few examples based on these two files.

Displaying The Default Tail Command Output

In this example, we will see how the tail command works without any arguments.

Example:

Output: Default tail Command Output

Since by default, the tail command displays the last 10 lines of a text file, the last 10 lines of "laureus-winners.txt" will be displayed.

Displaying Multiple Files with a Specified Number of Lines

In this example, we will understand how to display the last few lines of multiple files, as well as how to specify the number of lines to be displayed.

Example:

Output:

Multiple Files with a Specified Number of Lines Output

In the above example, the tail command is used with multiple text files, whereas the -n option is used to display the last 3 lines of each of the text files.

Monitoring Changes Using Tail Command

In this example, we will understand how the tail command is used to monitor changes in real-time using the -f option.

Example:

Output:

Monitoring Changes using tail Command Output

In the above example, the -n option is used to limit the number of lines that are displayed and the -f option is used to follow the file in real-time as new lines are added.

To return to the terminal, press CTRL+C.

Displaying Multiple Files Without Filenames

In this example, we will understand how to suppress file names, while displaying multiple files. In the above outputs, we can see that the file names are displayed along with the content when multiple files are displayed using the tail command. This can be changed using the -q option.

Example:

Output:

Displaying Multiple Files without Filenames

In the above example, the -n option displays the last 3 lines of both the text files, whereas the -q option suppresses the file name headers, resulting in both files being displayed together.

Using Tail Command with Pipes

The tail command can be used in conjunction with other commands with the use of pipes in Linux. This makes it possible to filter and display the last few lines of a command.

In this example, we are going to use the "grep" command and will redirect its output to the tail command with the help of pipes.

The grep command usually returns all the iterations of a pattern in a file.

Example:

Output:

Using tail Command with Pipes

But, with the help of pipes, we can use it with the tail command to manipulate the output.

Example:

Output:

Using tail Command with Pipes Output

In the above example, the output of the grep command is redirected to the tail command, which in this case is being used with option -n. This makes sure that no matter how many iterations of the specified pattern there may be, the maximum number of lines that will be displayed is limited to the number, specified by the tail command.

Conclusion

  • tail command is a versatile tool in Linux used for analyzing text files.
  • It displays the last part of a file or stream, making it suitable for monitoring log files and real-time updates.
  • tail command has several options, including customizing behaviour, searching for patterns, and following files in real-time.
  • With the help of pipes in Linux, the tail command can be used in conjunction with other commands to filter and display the last few lines of a file.