View and Follow the End of Text Files with tail
Updated by Linode Written by Linode
What is tail?
The tail
command is a core Linux utility used to view the end of text files. You can also use follow mode to see new lines as they’re added to a file in real time. tail
is similar to the head utility, used for viewing the beginning of files.
Syntax and Basic Usage
Tail uses the following basic syntax:
tail example.txt
This will print the last ten lines of example.txt
to standard output on the terminal. tail
is useful for reading files such as logs, where new content is appended to the end of the file.
To view multiple files, specify their names as additional arguments or use a wildcard:
tail example.txt example2.txt
==> example.txt <==
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
==> example2.txt <==
Line 1
Line 2
View the end of all .log
files in a directory:
tail *.log
Control the Length of tail Output
By default, tail
will output the final ten lines of a file. To view more or fewer than ten lines, use the -n [number]
option to control the number of lines that the tail
command prints:
tail -n 5 example.txt
Line 6
Line 7
Line 8
Line 9
Line 10
tail example.txt -n 2
Line 9
Line 10
Follow Mode
With the -f
option, tail
operates in follow mode. Here, tail
prints the final lines of a file, then watches for new additions to the end of the file. When new lines are added they are printed to the terminal, giving you a live feed of the end of the file.
tail
will continue to follow a file until the user sends a break (e.g. Control+c
) to the terminal. Additionally, if the file is deleted or renamed, tail -f
will fail. Use the -F
option to force tail
to follow file names rather than file objects. This can prevent problems with log rotation and other programs that may alter file names.
Follow mode is very useful when troubleshooting issues because it allows you to watch logs in real time.
Filter with grep
tail
can be combined with grep to filter the contents of a log file in real time. You can use this to track specific types of errors, such as 404 responses from an Apache web server:
tail -F /var/log/apache2/access.log | grep "404"
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.