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.
- A system running Linux.
- Access to the terminal (Ctrl+Alt+T).
Linux tail Syntax
tail command has the following syntax:
tail [options] [file]
- The available
[options]are described in the table below.
[file], specify a file or multiple files for
tailto process. By default,
taildisplays the last 10 lines of each
[file]to standard output, preceding their content with each file's name. Not specifying a
tailto read standard input.
Bear in mind that the input for
tail is case-sensitive.
Linux tail Options
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:
|Shows the last
NUM bytes of a file. Using
+ shows the bytes following from the specified
NUM byte of each file.
|Monitors file for changes and outputs new data as the file grows. When no value is specified after
descriptor is used as the default value. This means that the update mode continues to run even when the file is renamed or moved.
--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.
--pid=PID argument to exit
tail after the process with the
PID process ID terminates.
--follow= name --retry
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.
|Shows the last
NUM lines instead of the default 10. Using
-n +NUM causes the output to start with the line
|Omits the file names from the output, displaying only the contents.
|Used in combination with
tail to wait for
N seconds (default 1) between iterations.
tail always print the file name before displaying the contents.
NUL as the line delimiter instead of the newline character.
|Displays the help file.
|Prints the program version information.
tail command is designed to work primarily with ASCII text files, where one character corresponds to one byte. Using the
-n +NUM options when processing files in the Unicode character set can yield unexpected errors.
tail command usage through concrete examples in the sections below.
tail Command Examples
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
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.
The command outputs the last ten lines from the specified file.
Print Specific Number of Lines
-n option to print a different number of lines instead of the default ten. For example:
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:
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] …
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
--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.
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
-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:
Use Piped Input
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:
In the example above, the output shows the last five lines of the
ls command output.
Show Real-Time Updates
--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
Use the following syntax:
tail -f [file]
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
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.
In the example above, we take the last five lines from the employees.txt file and pipe the output to
sort command then rearranges the output in random order because we specified the
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
First, we redirect the output to the last-5-employees.txt file, and then use the
cat command to show the file's contents.
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.