What Is Linux tail Command and How to Use It With head Command

The Linux tail command displays the last few lines of one or more files as a standard output. It helps users efficiently retrieve new data for log monitoring, eliminating the need to open the files frequently.

System administrators commonly combine it with other Linux commands like sort or rm to manipulate the output. Moreover, they can add various options to modify the utility’s behavior based on the tasks.

To help improve your virtual private server (VPS) management efficiency, this article will explain Linux tail command basics. We will provide its syntax, share practical examples, and demonstrate how to combine it with the head utility.

The Linux tail command displays the last few entries of specified files as a standard output. By default, it outputs ten lines, but users can use an option to change it.

The tail command is commonly used for log file tracking and real-time update monitoring. System administrators combine it with other utilities using pipes to modify the output.

tail Command Syntax

By default, tail displays the 10 last lines of a file. Here’s the basic syntax:

tail [file_name]

You can use several options for customizing tail output. Here are some of the most popular ones with their long form and functions:

  • -c num or –bytes ‒ outputs the last num bytes of data.
  • -n num or –lines ‒ specifies the last num lines to show.
  • -f or –follow=name ‒ monitors new entries as the file grows.
  • -v or –verbose ‒ prints the file name before the data.
  • -q or –quiet, –silent ‒ omits the file name before printing the data.
  • -version ‒ displays the tail version information.

Important! Note that this Linux command syntax is case-sensitive.

tail Command Examples

In this section, we will explore the common uses and practical examples of the Linux tail command. To run this command on a VPS, connect using an SSH client like PuTTY or Terminal. Alternatively, Hostinger users can use our Browser terminal feature.

Suggested Reading

Haven’t bought a VPS hosting plan? Check out our article on the top VPS hosting providers to learn about the best options for your needs.

Using tail to Print the Last 10 Lines

The tail command’s basic functionality is outputting the last 10 entries from a single file. To do so, run it without any option or flag like the following example:

tail error.log

If the log file adds a new entry at the bottom, the tail command will output it as the last line. By default, it presents the data in descending order, starting from the newest addition.

The tail command prints the last 10 lines of a log file

You can also specify the directory path. Here are example commands for tracking the 10 latest entries of different web servers’ log files:

sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/nginx/access.log

Using tail to Print a Specific Number of Lines

To display specific lines with tail, add the -n option to your command. Here’s the syntax:

tail -n [num] [file_name]

See the following example command for printing the last two lines of the mynote.txt file:

tail -n 2 mynote.txt
The tail command prints the last two lines of a file

You can also omit the -n option and directly specify the number of lines to print as the flag, like this example:

tail -2 mynote.txt

Using tail to Specify the Number of Bytes

The -c option displays the last number of bytes of the specified file. Administrators commonly use it for regular-sized files containing the ASCII character set, where one character corresponds to one byte.

Here is the syntax:

tail -c [num] [file_name]

The following example outputs the last 50 bytes or characters of the mynote.txt file:

tail -c 50 mynote.txt

The -c option in the tail command counts bytes rather than characters. Therefore, when working with files using the Unicode character set, especially those with multi-byte characters, specifying the last num bytes may not align with character boundaries, leading to potentially confusing output.

Using tail to Sort Data

Combine other utilities and options with tail to query data in a specific order. The command differs depending on how you want to display the information.

For instance, pipe the ls command with -t and -l options to list 10 items within the current directory with the oldest modification timestamps. Here’s the syntax:

ls -tl | tail -n [number_of_lines]

You can also add the sort command with the -M option to reorder the output in ascending order based on the month of creation.

ls -tl | sort -M | tail -n [number_of_lines]

To reverse the output order, add the -r option to the tail command:

tail -n [number_of_lines] [file_name] | sort -r
The piped sort command reverses the tail output order

Using tail to Monitor a File for Changes

The –follow or -f option lets you run the tail command continuously, updating the output in real time whenever the specified file receives new data. Here’s the syntax:

tail -f [file_name]

The header line will highlight the changes in the original file. You can also specify the name as the option’s value like the following:

tail --follow=name [file_name]

If you don’t specify the name, the option will use the “descriptor” default value. It means the command will continue to print new lines from the same file during log rotation.

Even if you rename it or create a new file with the same name, the command will keep monitoring the same one. Using the uppercase -F option yields the same behavior.

Optionally, add the –max-unchanged-stats option to hide identical lines after a specific number of occurrences, simplifying Linux log monitoring. Here’s the command:

tail -f --max-unchanged-stats=5 error.log

To stop printing the new data, quit the update mode by pressing the Ctrl+C shortcut. A common tail -f usage includes real-time log viewing since new status information typically shows at the bottom of a file.

Pro Tip

For continuous log file analysis, run the tail -f command in another terminal instance using a multiplexer like Linux Screen to keep the process running in the background.

Using tail Command for Multiple Files

Processing multiple files simultaneously using the tail command requires you to list their names like the following:

tail [option] [file_name_1] [file_name_2] [file_name_3]

For instance, we will print five lines from multiple text files:

tail -n 5 file1.txt file2.txt file3.txt

If you run the tail command with multiple file names, Terminal will display headers specifying them.

The tail command prints the last five lines of multiple files

Hide the file header by adding the -q option to enable quiet mode, omitting the information:

tail -q [file_name]

Using tail to Track System Resource Usage

Combine the tail command with resource tracking utilities to monitor your system hardware usage. Here’s an example syntax:

command [interval] | tail -n [number_of_lines]

The resource tracking utility logs hardware usage at specified intervals. The tail command retrieves the data and outputs the last few lines.

For example, enter the following command to track the last five virtual memory usage logs, refreshing every second:

vmstat 1 | tail -n 5

To track your VPS I/O statistics, pipe the iostat command like the following:

iostat 1 | tail -n 5

Meanwhile, combine tail with the netstat command to monitor network-related resource usage:

netstat 1 | tail -n 5

If Terminal shows the “Command not found” error, install the resource tracking utilities using the following:

sudo apt install net-tools procps sysstat

Instead of commands, Hostinger VPS hosting users can check their server resource usage with our monitoring feature that offers a graphical user interface. To access it, go to hPanelBackup & MonitoringServer Usage.

Hostinger VPS resource monitoring dashboard in hPanel

Using tail Command With Pipes

In Linux operating systems, a pipe (|) is a standard output redirection. It lets a command pass its retrieved information to another utility as input.

In addition to file content, use a pipe to pass another utility’s output to the tail command. Here’s the syntax:

command | tail -n [number of lines]

For example, the netstat command prints many lines by default. However, piping its output to the tail utility lets you retrieve only the newest data for simpler analysis.

Using the head Command With Tail

The Linux head command prints the first few lines of one or multiple files as a standard output. Like the tail utility, it is a part of the GNU Coreutils. Here’s its basic syntax:

head -[option] [file_name]

You can use some of the tail command options with head. For example, add -n to print the first num lines from a file.

Combining head and tail commands allows you to output a specific section from a file.

For example, use the head command to extract the first lines from a file. Pipe the data as a standard input to tail to display the last few entries from that particular section. Consider these commands:

head -n 20 access.log | tail -10
head -n 20 error.log | tail -10

In the above examples, the head command retrieves the first 20 lines from the two log files and passes them through the pipe as input. Then, tail prints the last 10 lines.

The tail and head command output the last few lines of a file starting from the fifth

To extract the first num lines starting from a particular entry, specify the number to the tail command’s -n value. For example, here’s how to output entries starting from line number five:

head -n 10 mynote.txt | tail -5

Conclusion

The Linux tail command prints the last few lines of a file as a standard output. It helps simplify tasks like monitoring log files since system administrators don’t need to manually reopen them to check for new data.

This command-line utility retrieves the last 10 lines by default, but you can display a specified number of entries using the -n option. You can also specify multiple file names and print data based on their byte value.

Moreover, users can combine tail with other Linux command-line tools using a pipe (|) to modify the output for specific tasks. For example, add netstat, iostat, and vmstat to check the latest system resource usage log.

It’s also possible to combine it with the head utility to retrieve a particular section of a file. In addition, pipe the sort command to reorder the tail output based on your needs for simpler analysis.

Tail Command FAQ

In this section, we will answer several common questions about the Linux tail command to help expand your understanding of it.

How Do You Grep Tail Command?

Using the tail and grep combo requires turning on the line buffering mode. It allows the process to resume the search while reducing delay.

To do so, write tail with the -f option and the file name. Then, pipe the grep command with the –line-buffered option and the pattern to search.

How Do I Get Out of Tail Command in Linux?

To stop the tail command process and restore the terminal window, press Ctrl+C. This shortcut sends the SIGINT signal, telling the command-line interface to finish the process properly.

We don’t recommend using the SIGKILL signal since it forcefully terminates the process, potentially causing issues.

How Do I Redirect the Output of tail to a File?

To redirect the tail command output to a file, use the greater than symbol (>) and specify where you want to store the data. For example, running tail data.txt > log_file.txt will save data.txt’s last 10 entries into the log_file.txt file.

When redirecting output, ensure your current user has the write permission over the target file.

Author
The author

Jordana A.

Jordana is a Senior Content Writer with over 5 years of experience in digital marketing and web development. When she’s not busy with work, she dabbles in creative writing and movie reviewing. Follow her on LinkedIn.

Author
The Co-author

Aris B.

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.