Understanding the Tail Command in Linux with Examples

The Linux operating system has many powerful tools and utilities that allow you to perform complex tasks efficiently. One such tool is the “tail” command, which displays the last few lines of a file. This command is handy for system administrators and developers who need to monitor log files, configuration files, and other important files. In this article, we will delve into the tail command in Linux and provide a step-by-step guide with examples to help you understand how it works.

What is the Tail Command in Linux?

The tail command in Linux is a simple yet powerful tool that displays the last few lines of a file. By default, the tail command displays the last 10 lines of a file. This default can be changed by using the “-n” option followed by the number of lines you want to display. The tail command is handy when you want to keep an eye on the end of a file as it grows and updates the display in real time.

Before proceeding with how to use the tail command, first know the syntax of the tail command.

tail [OPTION]... [FILE]...

How to Use the Tail Command in Linux

Using the tail command in Linux is straightforward, as it only requires you to specify the file you want to display. For example, to display the last 10 lines of the “example.txt” file, enter the following command:

tail example.txt

You can also specify the number of lines you want to display using the “-n” option. For example, to display the last 20 lines of the “example.txt” file, enter the following command:

tail -n 20 example.txt

It’s worth noting that the tail command can also be used to display the last few lines of multiple files at once. For example, to display the last 10 lines of both the “file1.txt” and “file2.txt” files, enter the following command:

tail file1.txt file2.txt

Advanced Usage of the Tail Command in Linux

The tail command in Linux is not just limited to displaying the last few lines of a file. There are several advanced options and features that can be used to customize the output and make the command more useful.

Follow the File in Real-Time

One of the most useful features of the tail command is the ability to follow the file in real time. This is particularly useful when you want to monitor the end of a log file as it grows. Use the “-f” option to follow a file in real time. For example, to follow the “example.txt” file in real-time, enter the following command:

tail -f example.txt

Display the Entire File

If you want to display the entire file rather than just the last few lines, you can use the “-c” option, followed by the number of bytes you want to display. For example, to display the entire “example.txt” file, enter the following command:

tail -c +0 example.txt

Display the First Few Lines of a File

While the tail command is primarily used to display the last few lines of a file, it can also be used to display the first few lines of a file. To display the first few lines of a file, use the “head” command. The head command works similarly to the tail command, but instead of displaying the last few lines, it displays the first few lines of a file.

For example, to display the first 10 lines of the “example.txt” file, enter the following command:

head -n 10 example.txt

You can also combine the head and tail commands to display a specific range of lines in a file. For example, to display lines 11 to 20 of the “example.txt” file, enter the following command:

head -n 20 example.txt | tail -n 10

Examples of Working with Tail Commands

Displaying the Names of American States and their Capitals using the Tail Command in Linux

The tail command in Linux can display the contents of multiple files at once. This can be useful when you have related information stored in separate files and want to display the information together.

For example, let’s consider two files named “states.txt” and “capitals.txt” that contain the names of American states and their capitals, respectively. The “states.txt” file might look something like this:

Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia

And the “capitals.txt” file might look something like this:

Montgomery
Juneau
Phoenix
Little Rock
Sacramento
Denver
Hartford
Dover
Tallahassee
Atlanta

To display the names of American states and their capitals together, we can use the tail command to display the contents of both files. To do this, enter the following command:

paste states.txt capitals.txt | tail

Example Output:

Georgia
Atlanta

As you can see, the tail command displays the contents of both files, and the “paste” command combines the contents of the two files into a single output. The result is a display of the names of American states and their capitals, with each state and capital appearing on the same line.

Advanced Options of the Tail Command in Linux with American State and Capital Examples

In addition to displaying the last few lines of a file, the tail command in Linux offers several advanced options that can be used to customize the output. Let’s consider two files named “states.txt” and “capitals.txt” that contain the names of American states and their capitals, respectively.

tail -n 3 states.txt

Example output:

West Virginia
Wisconsin
Wyoming

You can also use the “-n” option without the “n” character as long as you use the “-” symbol. For example:

tail -3 states.txt

The tail command also has an option “+,” which is not present in the head command. With this option, the tail command prints the data starting from a specified line number in the file instead of from the end of the file. For the command “tail +n file_name,” the data will start printing from line number “n” until the specified file’s end.

tail +25 states.txt

Example Output:

Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming

Displaying a Specific Number of Bytes

The “-c” option can display a specific number of bytes from the end of a file. For example, if you want to display the last 6 bytes of the “states.txt” file, enter the following command:

tail -c -6 states.txt

Example output:

oming

Alternatively, if you want to display all the data after skipping 263 bytes from the start of the “states.txt” file, enter the following command:

tail -c +263 states.txt

Suppressing File Names

When displaying the contents of multiple files, the tail command will display the name of each file before the contents of that file. To suppress the file names, use the “-q” option. For example, enter the following command to display the contents of both the “states.txt” and “capitals.txt” files without file names:

tail -q states.txt capitals.txt

Example Output:

West Virginia
Wisconsin
Wyoming
Charleston
Cheyenne

Displaying File Names

If you want to display the name of each file before its contents, use the “-v” option. For example, enter the following command to display the name of the “states.txt” file before its contents:

tail -v states.txt

Example output:

==> states.txt <==
West Virginia
Wisconsin
Wyoming

Showing the Version of Tail

To display the version of the tail command currently running on your system, use the “–version” option. For example, enter the following command:

tail --version

Example output:

tail (GNU coreutils) 9.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Ian Lance Taylor,
and Jim Meyering.

Monitoring Log Files

System administrators commonly use the “-f” option to monitor log files written by Unix programs as they are running. This option displays the last ten lines of a file and will update as new lines are added. For example, if you have a log file named “logfile,” enter the following command to monitor it:

tail -f logfile

Using Tail with Pipes

The tail command can be combined with other Unix commands using pipes. For example, you can pipe the output of the tail command into the sort command to sort the last 7 state names from the “states.txt” file in reverse order:

tail -n 7 states.txt | sort -r

Example Output:

Wyoming
Wisconsin
West Virginia
Washington
Virginia
Vermont
Utah

You can also use multiple filters for additional processing. For example, you can use the cat, head, and tail commands to store the output in a file named “list.txt.”

cat states.txt | head -n 20 | tail -n 5 > list.txt

The above command first uses the cat command to display the contents of the “states.txt” file, then pipes the output to the head command, which displays the first 20 lines. The output from the head command is then piped to the tail command, which displays the last 5 lines, and finally, the output is stored in the file “list.txt” using the directive operator.

Conclusion

The tail command in Linux is a simple yet powerful tool that allows you to display the last few lines of a file. Whether you’re a system administrator or a developer, the tail command is essential in your toolkit. By following this guide, you should now understand how to use the tail command in Linux, as well as some of the advanced features and options it offers.

Share to...