
tail Command in Linux
The tail command in Linux is used to display the last part of the specified file. By default, it shows the last 10 lines but can be adjusted to display a specific number of lines or bytes. It is particularly efficient for large files as it only reads the end portion of the file. This command is commonly used in system administration, troubleshooting, debugging, and for tracking live events. It works well with other tools, such as grep, for filtering output.
Table of Contents
Here is a comprehensive guide to the options available with the tail command −
Syntax of tail Command
The syntax of the tail command in Linux is as follows −
tail [optionsâ¦] [fileâ¦]
The [optionsâ¦] field is used to specify one or more options that modify the command's output. The [fileâ¦] field specifies one or more files to process.
tail Command Options
The options for the tail command are as follows −
Short Option | Long Option | Description |
---|---|---|
-c K | --bytes=K | Show the last K bytes; use +K to start from the Kth byte. |
-f | --follow[={name|descriptor}] | Output appended data as file grows. |
-F | Same as --follow=name --retry. | |
-n K | --lines=K | Show the last K lines; use +K to start from the Kth line. |
--max-unchanged-stats=N | With --follow=name, reopen the file after N unchanged checks. | |
--pid=PID | With -f, stop after the specified PID terminates. | |
-q | --quiet, --silent | Suppress file name headers. |
--retry | Keep trying to open inaccessible files (used with --follow=name). | |
-s N | --sleep-interval=N | With -f, wait ~N seconds between reads (default: 1.0s). |
-v | --verbose | Always show file name headers. |
--help | Show help and exit. | |
--version | Show the version and exit. |
Examples of tail command in Linux
The section discusses how to use the tail command in Linux with examples −
- Displaying the Last 10 Lines of a File
- Displaying the Specific Last Lines of a File
- Displaying from Specific Lines and Onward
- Displaying the Last Lines from Multiple Files
- Displaying Last Specific Bytes
- Following the File as it Grows
- Following a Log File with Retry
- Suppressing File Name Headers
- Using tail Command with Pipe
- Displaying Usage Help
Displaying the Last 10 Lines of a File
To display the last 10 lines of a file, use the tail command followed by the file name −
tail file.txt

Displaying the Specific Last Lines of a File
To display the specific number of the last lines of a file, use the tail command with the -n or --lines option. For example, to display the last 5 lines of a file, use the following command −
tail -n 5 file.txt

Or, simply use −
tail -5 file.txt

Displaying from Specific Lines and Onward
To display lines from a specific starting point onward, use the -n or --lines option with the + sign followed by the starting line number. For example, to display lines from line 5 to the last line. For example, to display line 5 to the last line, use the tail command in the following way −
tail -n +5 file.txt

Note that the + sign is used for starting from a specific line, not just displaying from it.
Displaying the Last Lines from Multiple Files
To display last lines from multiple files, use the tail command in the following way −
tail -n +8 file1.txt file2.txt

Displaying Last Specific Bytes
The -c or --byte option makes the tail command use byte offsets instead of lines. It is useful for ASCII data with fixed-size records, where each character and newline count as one byte. For example, to print the last 40 bytes of the specified file −
tail -c 120 file.txt

Note that it counts byte-by-byte, which might not align with character boundaries in multi-byte encodings.
Following the File as it Grows
To continuously show new lines added to a file, use the -f or --follow option −
tail -f /var/log/syslog
It is useful to view the log files in real-time.
To specify the delay between reads, use the -s or --sleep-interval option to specify the delay time. For example, to set the wait time to 3 seconds, use the following command −
tail -f -s 3 /var/log/syslog
Following a Log File with Retry
To read the log file with auto-retry, use the -F option. It is equivalent to -f --retry −
tail -F /var/log/syslog
The --retry works with --follow to handle cases where files may be temporarily inaccessible, particularly for logs that are rotated.
Suppressing File Name Headers
To suppress file name headers when displaying output from multiple files, use the -q, --quiet, or --silent option −
tail -q -n +8 file1.txt file2.txt

Using tail Command with Pipe
The tail command can read piped input from other commands. For example, use it with cat or ls to display the last five lines of their output −
ls -l | tail -5

Displaying Usage Help
To display the usage help, use the --help option −
tail --help
Conclusion
The tail command in Linux is a useful tool for viewing the end portion of files, especially large ones. By default, it shows the last 10 lines but can be adjusted to show a specific number of lines or bytes. It supports real-time monitoring of file changes and works well with other commands through piping.
Commonly used in troubleshooting and system monitoring, the tail command offers various options to control its output, making it a flexible and efficient command-line utility.