‘tail’: Output the last part of files

‘tail’ prints the last part (10 lines by default) of each FILE; it reads from standard input if no files are given or when given a FILE of ‘-‘.

tail [OPTION]… [FILE]…

If more than one FILE is specified, ‘tail’ prints a one-line header consisting of:

==> FILE NAME <==

before the output for each FILE.

GNU ‘tail’ can output any amount of data (some other versions of ‘tail’ cannot).  It also has no ‘-r’ option (print in reverse), since
reversing a file is really a different job from printing the end of a file; BSD ‘tail’ (which is the one with ‘-r’) can only reverse files
that are at most as large as its buffer, which is typically 32 KiB.  A more reliable and versatile way to reverse files is the GNU ‘tac’

The program accepts the following options.

'-c K' | '--bytes=K'
Output the last K bytes, instead of final lines.  However, if K  starts with a '+', start printing with the Kth byte from the start of each file, instead of from the end.  K may be, or may be an integer optionally followed by, one of the following multiplicative
          'b'  =>            512 ("blocks")
          'KB' =>           1000 (KiloBytes)
          'K'  =>           1024 (KibiBytes)
          'MB' =>      1000*1000 (MegaBytes)
          'M'  =>      1024*1024 (MebiBytes)
          'GB' => 1000*1000*1000 (GigaBytes)
          'G'  => 1024*1024*1024 (GibiBytes)
and so on for 'T', 'P', 'E', 'Z', and 'Y'.

'-f' | '--follow[=HOW]'
Loop forever trying to read more characters at the end of the file, presumably because the file is growing.  If more than one file is given, 'tail' prints a header whenever it gets output from a different file, to indicate which file that output is from.


This option is the same as '--follow=name --retry'.  That is, tail will attempt to reopen a file when it is removed.  Should this fail, tail will keep trying until it becomes accessible again.


Indefinitely try to open the specified file.  This option is useful  mainly when following (and otherwise issues a warning).

When following by file descriptor (i.e., with  '--follow=descriptor'), this option only affects the initial open
of the file, as after a successful open, 'tail' will start following the file descriptor.

When following by name (i.e., with '--follow=name'), 'tail' infinitely retries to re-open the given files until killed.

Without this option, when 'tail' encounters a file that doesn't exist or is otherwise inaccessible, it reports that fact and never checks it again.


Change the number of seconds to wait between iterations (the  default is 1.0).  During one iteration, every specified file is  checked to see if it has changed size.  Historical implementations of 'tail' have required that NUMBER be an integer.  However, GNU 'tail' accepts an arbitrary floating point number.  

When 'tail' uses inotify, this polling-related option is usually ignored.  However, if you also specify '--pid=P', 'tail'  checks whether process P is alive at least every NUMBER seconds.


When following by name or by descriptor, you may specify the process ID, PID, of the sole writer of all FILE arguments.  Then, shortly after that process terminates, tail will also terminate. This will work properly only if the writer and the tailing process  are running on the same machine.  For example, to save the output of a build in a file and to watch the file grow, if you invoke 'make' and 'tail' like this then the tail process will stop when  your build completes.  Without this option, you would have had to kill the 'tail -f' process yourself.

          $ make >& makerr & tail --pid=$! -f makerr

If you specify a PID that is not in use or that does not correspond to the process that is writing to the tailed files, then 'tail' may  terminate long before any FILEs stop growing or it may not terminate until long after the real writer has terminated.  Note that '--pid' cannot be supported on some systems; 'tail' will print a warning if this is the case.

'-n K' | '--lines=K'
Output the last K lines.  However, if K starts with a '+', start printing with the Kth line from the start of each file, instead of from the end.  Size multiplier suffixes are the same as with the  '-c' option.

'-q' | '--quiet'|'--silent'

Never print file name headers.

'-v' | '--verbose'
Always print file name headers.

For compatibility ‘tail’ also supports an obsolete usage ‘tail  -[COUNT][bcl][f] [FILE]’, which is recognized only if it does not conflict with the usage described above.  This obsolete form uses exactly one option and at most one file.  In the option, COUNT is an optional decimal number optionally followed by a size letter (‘b’, ‘c’,’l’) to mean count by 512-byte blocks, bytes, or lines, optionally followed by ‘f’ which has the same meaning as ‘-f’.

On older systems, the leading ‘-‘ can be replaced by ‘+’ in the obsolete option syntax with the same meaning as in counts, and obsolete usage overrides normal usage when the two conflict.  This obsolete behavior can be enabled or disabled with the ‘_POSIX2_VERSION’  environment variable

Scripts intended for use on standard hosts should avoid obsolete syntax and should use ‘-c COUNT[b]’, ‘-n COUNT’, and/or ‘-f’ instead.

If your script must also run on hosts that support only the obsolete syntax, you can often rewrite it to avoid problematic usages,

e.g., by using ‘sed -n ‘$p” rather than ‘tail -1’. 

If that’s not possible, the script can use a test like

if tail -c +1 </dev/null >/dev/null 2>&1; then …

to decide which syntax to use.

Even if your script assumes the standard behavior, you should still beware usages whose behaviors differ depending on the POSIX version.

For example, avoid  'tail - main.c',

since it might be interpreted as either

'tail main.c' or as 'tail -- - main.c';

avoid 'tail -c 4', since it might mean either

'tail -c4' or 'tail -c 10 4';

and avoid 'tail +4', since it might mean either

'tail ./+4' or 'tail -n +4'.

An exit status of zero indicates success, and a nonzero value  indicates failure.



I have started unixadminschool.com ( aka gurkulindia.com) in 2009 as my own personal reference blog, and later sometime i have realized that my leanings might be helpful for other unixadmins if I manage my knowledge-base in more user friendly format. And the result is today's' unixadminschool.com. You can connect me at - https://www.linkedin.com/in/unixadminschool/

What is in your mind, about this post ? Leave a Reply

  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us