Linux tail Command (with Examples)

January 30, 2023

Introduction

The Linux tail command is a command-line utility that prints data from the end of a specified file or files to standard output. The utility provides an easy way to quickly see file updates in real time, as new data is usually added to the end of a file.

For example, the tail command is useful for monitoring various textual log files, such as log files of web servers like Apache or Nginx.

In this tutorial, you will learn to use the Linux tail command through examples.

Linux tail command tutorial.

Prerequisites

  • A system running Linux.
  • Access to the terminal (Ctrl+Alt+T).

Linux tail Syntax

The tail command has the following syntax:

tail [options] [file]
  • The available [options] are described in the table below.
  • For [file], specify a file or multiple files for tail to process. By default, tail displays the last 10 lines of each [file] to standard output, preceding their content with each file's name. Not specifying a [file] causes tail to read standard input.

Bear in mind that the input for tail is case-sensitive.

Note: The tail command complements the head command, which displays the beginning of a file. Both commands ignore the rest of the file. To print the entire file contents, use the cat command.

Linux tail Options

The tail command options allow users to customize the output and change how tail processes the file. Since tail is part of the GNU Coreutils package, most options have a short and long form.

The table below shows the available options and their descriptions:

Short FormLong FormDescription
-c--bytes=[+]NUMShows the last NUM bytes of a file. Using + shows the bytes following from the specified NUM byte of each file.
-f--follow[={name|descriptor}]Monitors file for changes and outputs new data as the file grows. When no value is specified after --follow=, descriptor is used as the default value. This means that the update mode continues to run even when the file is renamed or moved.
Specify the --max-unchanged-stats=N argument to reopen a [file] that has not changed size after N (default 5) iterations to check if it has been unlinked or renamed.
Specify the --pid=PID argument to exit tail after the process with the PID process ID terminates.
-F--follow= name --retryInstructs tail to keep updating the output even if the original file is removed during the log rotation and replaced by a new one with the same name.
-n--lines=[+]NUMShows the last NUM lines instead of the default 10. Using -n +NUM causes the output to start with the line NUM.
-q--quiet, --silentOmits the file names from the output, displaying only the contents.
-s--sleep-interval=NUsed in combination with -f. Instructs tail to wait for N seconds (default 1) between iterations.
-v--verboseMakes tail always print the file name before displaying the contents.
-z--zero-terminatedUses NUL as the line delimiter instead of the newline character.
--helpDisplays the help file.
--versionPrints the program version information.

Note: The tail command is designed to work primarily with ASCII text files, where one character corresponds to one byte. Using the -c or -n +NUM options when processing files in the Unicode character set can yield unexpected errors.

Explore the tail command usage through concrete examples in the sections below.

tail Command Examples

The tail command has different applications that vary from displaying live file updates to processing multiple files and merging their output into a single one. The examples below showcase the most common uses of tail.

Print Last 10 Lines

By default, if no options are specified, the tail command prints the last 10 lines of a file to standard output. Specify the file path and the command shows the end of the file.

For example:

Example of a tail command output.

The command outputs the last ten lines from the specified file.

Print Specific Number of Lines

Use the -n option to print a different number of lines instead of the default ten. For example:

Print a specific number of lines with tail.

Alternatively, specify the n +NUM option to print the contents starting from line NUM. In the following example, the output contains lines starting from line 19 until the end of the file:

Start printing file contents from a specific line.

Print Lines from Multiple Files

Work with multiple files at once by passing the file names after the tail command. For example, to print the last two lines of multiple files, use the following syntax:

tail -n 2 [file1] [file2] [file3] …
Print lines from multiple files using tail.

The command prints the last two lines of the two specified files, prepending each one with a header containing the file name.

Merge Contents from Multiple Files

Use the -q (--quiet or --silent) option with tail to merge the output of multiple files. When the -q option is specified, the headers with file names are removed, merging the contents of all files together.

For example:

Merge the contents of multiple files in Linux.

The command prints the last five lines from both specified files without specifying the file names and merges them together.

Use Bytes Instead of Lines

Specifying the -c option instructs tail to use offsets in bytes instead of lines. Use this feature for ASCII data (one byte=one character) formatted into regular-sized records. The newline character also counts as one byte.

In the following example, we print the last 24 bytes of the specified file:

Print a specific number of bytes instead of lines.

Use Piped Input

The tail command accepts piped input from other commands instead of specifying files. For example, pipe input from the cat command, or from the ls command, and show the last five lines of the output:

Pipe the ls command output to tail.

In the example above, the output shows the last five lines of the ls command output.

Show Real-Time Updates

The -f (--follow) option allows users to track files in real time and show any updates in the standard output. The option is usually used for log files because any new status information is usually added to the end of the file. However, the log file must be a text file to be readable by tail.

Use the following syntax:

tail -f [file]

For example:

Tracking updates to the dpkg log file in Linux.

The command above outputs the log for the dpkg package manager and waits for new data to be added to the file. To abort the command, press the interrupt key combination (Ctrl+C). The default refresh interval is 1 second. To specify a different refresh interval, use the -s option.

The syntax is:

tail -f -s <sleep interval in seconds> [file]

Note: Another command that allows you to track updates in real time is less. Open the file in less and press Shift+F to enter the update mode.

Arrange tail Output

Apart from taking input through pipes, the tail command output can also be piped to other commands, such as the sort command, to arrange it in a specific order.

For example:

Piping the tail command output to the sort command.

In the example above, we take the last five lines from the employees.txt file and pipe the output to sort. The sort command then rearranges the output in random order because we specified the -R option.

Save Output to File

If you want to quickly extract part of a file and save it for later, redirect the tail output to a new file using the > character.

For example:

Saving tail output in a file and reading it with the cat command.

First, we redirect the output to the last-5-employees.txt file, and then use the cat command to show the file's contents.

Conclusion

This tutorial showed how to use the Linux tail command. It is a useful CLI utility that provides efficient file management capabilities, allowing users to work with content from single or multiple text files. Additionally, the command facilitates the monitoring of log files and file updates.

Learn more crucial Linux commands and download a handy cheat sheet PDF from our website.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How To Use the touch Command in Linux
November 22, 2021

The touch command creates new files. However, advanced options deal with file timestamps. Follow this tutorial to learn how to use touch in the command line.
Read more
How to Remove (Delete) a File or Directory in Linux
August 8, 2019

This article lists the most commonly used commands and tools to remove unwanted files and directories from your system.
Read more
How To Use grep Command In Linux/UNIX
March 28, 2019

This guide details the most useful grep commands for Linux / Unix. After going through the examples, you will know to use grep to search files for text.
Read more
How to Use the Linux head Command
January 5, 2022

The head commands lists the first ten lines of a file in standard output. Learn how to use the head command and its options (with examples).
Read more