Linux tail command

Updated: 03/13/2021 by Computer Hope
tail command

On Unix-like operating systems, the tail command reads a file, and outputs the last part of it (the "tail").

The tail command can also monitor data streams and open files, displaying new information as it is written. For example, it's a useful way to monitor the newest events in a system log in real time.

This page covers the GNU/Linux version of tail.

Description

By default, tail prints the last 10 lines of each file to standard output. If you specify more than one file, each set of output is prefixed with a header showing the file name.

If no file is specified, or if file is a dash ("-"), tail reads from standard input.

Syntax

tail [{-c |--bytes=}num] [-f] [--follow[={name|descriptor}]] 
     [-F] [{-n |--lines=}num] [--max-unchanged-stats[=num]] 
     [--pid=pid] [{-p|--quiet|--silent}] [--retry] 
     [{-s |--sleep-interval=}num] [{-v|--verbose}] [file ...]
tail --help
tail --version

Options

Option Description
-c [+]num,
--bytes=[+]num
Output the last num bytes of each file.

You can also use a plus sign before num to output everything starting at byte num. For instance, -c +1 prints everything.

A multiplier suffix can be used after num to specify units: b (512), kB (1000), K (1024), MB (1000*1000), M (1024*1024), GB (1000*1000*1000), G (1024*1024*1024), and so on for T (terabyte), P (petabyte), E (exabyte), Z (zettabyte), Y (yottabyte).
-f,
--follow[={name|descriptor}]
This option causes tail to loop forever, checking for new data at the end of the file(s). When new data appears, it will be printed.

If you follow more than one file, a header will be printed to indicate which file's data is being printed.

If the file shrinks instead of grows, tail lets you know with a message.

If you specify name, the file with that name is followed, regardless of its file descriptor.

If you specify descriptor, the same file is followed, even if it's renamed. This is the default behavior.
-F "Follow and retry". Same as using --follow=name --retry.
-n num,
--lines=num
Output the last num lines, instead of the default (10).

If you put a plus sign before num, tail outputs all lines beginning with that line. For example, -n +1 prints every line.
--max-unchanged-stats=num If you are following a file with -f or --follow=name, tail continuously checks the file to see if its size has changed. If the size has changed, it reopens the file and looks for new data to print. The --max-unchanged-stats option reopens a file, even if its size has not changed, after every num checks.

This option is useful if the file might be spontaneously unlinked or renamed, such as when log files are automatically rotated.
--pid=pid When following with -f or --follow, terminate operation after process ID pid dies.
-q,
--quiet,
--silent
Never output headers.
--retry Keep trying to open a file even if it's temporarily inaccessible; useful with the --follow=name option.
-s num,
--sleep-interval=num
When following with -f or --follow, sleep for approximately num seconds between file checks. With --pid=pid, check process pid at least once every num seconds.
-v,
--verbose
Always print headers.
--help Display a help message, and exit.
--version Display version information, and exit.

Examples

tail myfile.txt

Outputs the last 10 lines of the file myfile.txt.

tail -n 100 myfile.txt

Outputs the last 100 lines of the file myfile.txt.

tail -f myfile.txt

Outputs the last 10 lines of myfile.txt, and monitors myfile.txt for updates; tail then continues to output any new lines that are added to myfile.txt.

Tip

The tail command follows the file forever. To stop it, press Ctrl+C.

tail -f access.log | grep 24.10.160.10

This is a useful example of using tail and grep to selectively monitor a log file in real time.

In this command, tail monitors the file access.log. It pipes access.log's final ten lines, and any new lines added, to the grep utility. grep reads the output from tail, and outputs only those lines which contain the IP address 24.10.160.10.

cat — Output the contents of a file.
head — Display the first lines of a file.
more — Display text one screen at a time.
pg — Browse page by page through text files.