tail command or tail command It will display the last (10 lines by default) of one or more files or data being piped. This command can also be used to monitor file changes in real time.
One of the most common uses of the tail command is to watch and analyze logs and other files that change over time, usually in conjunction with other tools like grep.
In this tutorial, we’ll show you how to use the Linux tail command with practical examples and step-by-step explanations of the most common tail options.
Basic tail command syntax
Before discussing how to use the tail command, let’s start by reviewing the basic syntax.
For basic commands, the Tail Command has the following format:
tail [OPTION]... [FILE]...
OPTION
Tail options. We’ll cover the most popular options in the next section.FILE
– In the form of one or more input file names. If FILE is not specified, or when FILE is-
the tail will read the standard input.
How to use Tail Command
In its simplest form when used without any options, the tail command will produce the last 10 lines.
tail namafile.txt
How to display a certain number of rows
Use option -n
(--lines
) to specify the number of rows to display:
tail -n <NUMBER> namafile.txt
You can also omit the letter n and just use the dash (-
) and a number (without spaces in between).
For example to display the last 50 lines of a file named namafile.txt
can be used:
tail -n 50 namafile.txt
The following example will return the same result as the above command:
tail -50 namafile.txt
How to display the number of bytes
To display a specified number of bytes, use Options -c
(--bytes
).
tail -c <NUMBER> namafile.txt
For example to display the last 500 bytes of data from a named file namafile.txt
Then use the command:
tail -c 500 namafile.txt
You can also use a multiplication suffix after a number to specify the number of bytes to display. using the suffix b
Then the same multiplication by 512, kB
multiply by 1000 K
Multiply by 1024 MB
Multiply by 1000000 M
Multiplying 1048576 and so on.
The following command will output the last 2 KB (2048) of the file namafile.txt
:
tail -c 2k namafile.txt
How to view changes in a file
To monitor changes in files, use Options -f
(--follow
):
tail -f namafile.txt
This option is very useful for monitoring log files. For example, to display the last 10 lines of a file /var/log/nginx/error.log
and monitor the file for updates that you will use:
tail -f /var/log/nginx/error.log
To stop the tail command while observing a file, press Ctrl+C
.
To keep monitoring files while they are being recreated, use Options -F
.
tail -F namafile.txt
This option is useful in situations where the tail command follows a rotating log file. When used with options -F,
The tail command will reopen the file as soon as it is available again.
How to view multiple files
If multiple files are provided as input to the tail command, it will output the last ten lines of each file.
tail namafile1.txt namafile2.txt
You can use the same options when viewing a single file.
This example shows the last 20 lines of the file namafile1.txt
And the namafile2.txt
:
tail -n 20 namafile1.txt namafile2.txt
Combine tails and other commands
The tail command can be used with other commands by redirecting standard output to/from other utilities using pipes (|).
For example to monitor apache’s access log file and only show lines containing IP addresses 192.168.232.22
Then the command you will use:
tail -f /var/log/apache2/access.log | grep 192.168.232.22
The following command will display the top ten running processes in order of CPU usage:
ps aux | sort -nk +3 | tail -5
conclusion
By now you should have a good understanding of how to use the tail command in Linux. This command is a complement to the head command which prints the first line of the file to the terminal.