Softpanorama

May the source be with you, but remember the KISS principle ;-)
Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

tail command

News Unix Utilities Recommended Links head command Examples Linux logtail command since command
tac command Syslog viewers Syslog analyzers Log Colorizing Syslog MultiTail Logwatch Syslog Anomaly Detection Analyzers
Multitail Syslog tips sort Perl One-Liners AWK Humor Etc

Unix tail  command is a classic Unix command. It is a standard way of checking the last lines as well as dynamically watching log files in Unix. The  tail  command first appeared in Version 7 of AT&T UNIX.  The complementary utility head is used for getting the first N lines of the file. There is also less well known, but more useful for log watching utility called since  which is distributed with Cygwin package and can be compiled on any Unix platform.  It is capable of remembering the last line of the file viewed so only new lines are displayed.

GNU tail is much more capable than many people (even seasoned sysadmins) think.

By default, tail prints the last 10 lines. You can specify a specific number of lines with the --lines=n (or -n n) switch.

$ tail -n 50 /var/log/messages

You can abbreviate the -n switch to a minus sign and the number of lines.

$ tail -50 /var/log/messages

Combining tail and head in a pipeline, you can display any line or range of lines.

$ head -5000 /var/log/messages | tail -100

If the starting line is a plus sign instead of a minus sign, tail counts that number of lines from the start of the file and prints the remainder. This is a feature of tail, not the head command.

$ tail +1700 /var/log/messages

When using head or tail on arbitrary files in a script, always check to make sure that the file is a regular file to avoid unpleasant surprises.

For small files since  can be imitated with the following shell script:

if  [ -e ~/.since ] ; then
    PREV=`grep " $1$"  ~/.since | cut -d' ' -f 1`
fi
grep -v  " $1$" ~/.since >  ~/.since
wc -l $1 >>  ~/.since
tail +$($PREV:0) $1

Similar Perl script called logtail  is available on most Linux distributions.

By default tail  displays the last 10 lines. For watching logs tail  has a special command line option -f  (follow). It allows a file to be monitored. Instead of displaying the last few lines and exiting, tail  displays the lines and then continue to monitors the file. As new lines are added tail  updates the display. This is particularly useful for monitoring log files. For example:

tail -f /var/adm/messages

Using options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The syntax is:

tail  [-f | -r] [-b number | -c number | -n number | -number | + number] [file ...]

The tail  utility displays the contents of file or, by default, its standard input, to the standard output. The display begins at a byte, line, or 512-byte block location in the input. Default is lines but bytes count is possible

Typically tail is used with "minus" operation which means that the lines are counted from the end of the file. But in GNU version of tail this is actually only one of two possibilities and tail can count lines from the beginning too. Few people know about this. Still it is important to understand that:

Other options are used less often. Among them:

If more than a single file is specified, each file is preceded by a header consisting of the string ``==> XXX <=='' where ``XXX'' is the name of the file.

The tail  utility exits 0 on success or >0 if an error occurred.

Examples

To display the last 500 lines of the file foo:

tail -500 foo 

Keep /var/log/messages open, displaying to the standard output anything appended to the file:

tail -f /var/log/messages

Frequently used variants often can be made into alias, for example:

alias mess='tail -100 /var/log/messages | more'

See Also

Standards

The tail  utility is expected to be a superset of the IEEE Std1003.2-1992 (``POSIX.2'') specification. In particular, the -b and -r options are extensions to that standard.

The historic command line syntax of tail  is supported by this implementation. The only difference between this implementation and historic versions of tail, once the command line syntax translation has been done, is that the -b, -c and -n options modify the -r option, i.e., -r -c 4 displays the last 4 characters of the last line of the input, while the historic tail (using the historic syntax -4cr) would ignore the -c option and display the last 4 lines of the input.

 


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Jan 19, 2011] MultiTail stable version 5.2.7!

Also at MultiTail freshmeat.net

MultiTail lets you view one or multiple files like the original tail program. The difference is that it creates multiple windows on your console (with ncurses). It can also monitor wildcards: if another file matching the wildcard has a more recent modification date, it will automatically switch to that file. That way you can, for example, monitor a complete directory of files. Merging of 2 or even more logfiles is possible. It can also use colors while displaying the logfiles (through regular expressions), for faster recognition of what is important and what not. It can also filter lines (again with regular expressions). It has interactive menus for editing given regular expressions and deleting and adding windows. One can also have windows with the output of shell scripts and other software.

When viewing the output of external software, MultiTail can mimic the functionality of tools like 'watch' and such.
For a complete list of features, look here.

[Jan 27, 2010] SINCE(1)

This very useful utility is distributed with Cygwin.

since - display content of a file since the last time

since [-hnqvx] files

DESCRIPTION

since is similar to tail since it also displays the tail of a file. However since displays only the data which has been added since the last time since was run. If since has not been run before the entire file is displayed. since is useful for watching over log files.

EXAMPLE

since /var/log/apache/{access,error}_log > /dev/null
lynx --dump http://localhost/ > /dev/null
since /var/log/apache/{access,error}_log

OPTIONS

FILES

.since

File recording the length of the files displayed previously. The location of the file can be set by using the SINCE environment variable, for example export SINCE= /var/log/sincefile will tell since to use the file /var/log/sincefile. If the SINCE

inotail

inotail is a replacement for the 'tail' program found in the base installation of every Linux/UNIX system. It makes use of the inotify infrastructure in recent versions of the Linux kernel to speed up tailing files in the follow mode (the '-f' option). Standard tail polls the file every second by default while inotail listens to special events sent by the kernel through the inotify API to determine whether a file needs to be reread.

Currently inotail is not fully compatible to neither POSIX or GNU tail but might be in the future. (Note: I'm currently working on correct tailing from pipes which would make inotail fully compatible to POSIX tail)

2007-09-07 - inotail 0.5 released

Released version 0.5 of inotail containing only small fixes. Please refer to the changelog for details.

download

Debian users can find the inotail package in the Debian package archive. inotail is also available in the Ubuntu Universe repository.

PPT tail

#!/usr/bin/perl -w

#
# A Perl implementation of tail for the Perl Power Tools project by
# Thierry Bézecourt <thbz@worldnet.fr>. Includes an implementation
# of Chip Rosenthal's xtail.
#
# Please see the pod documentation at the end of this file.
#
# 99/12/24 : made tail -f work on VMS (thanks to Joe Kazimierczyk)
# fixed a bug when tailing small files
# 99/11/19 : important performance improvement for tail on big files
# (now usable for any file size)
# 99/03/07 : implemented xtail in the -f option
# 99/03/03 : fixed the -f option which was completely broken
# 99/03/02 : first version

PPT tail

This is the homepage for the Perl Power Tools implementation of the standard tail command.

Current Perl implementations are: Baseline documentation:

Perl Tail

#!/usr/bin/perl

use File::Tail;
$file=File::Tail->new( $file_to_tail );
while (defined($line=$file->read))
{
print $line;
}

The tail Command

The output from other commands can be piped (i.e., sent) to tail to use as its input. For example, the following sends the output from the ls command (which by default lists the names of the files and directories in the current directory) to tail, which, in turn, prints the final ten lines of the output that it receives from ls to the monitor screen:

ls | tail

This output could easily be redirected, for example to a file named last_filenames as follows:

ls | tail >> last_filenames

It could also be piped to one or more filters for additional processing. For example, the sort filter could be used with its -r option to sort the output in reverse alphabetic order prior to writing to a file:

ls | tail | sort -r >> last_filenames

The -q (i.e., quiet) option causes tail to not print the file name before each set of lines and to eliminate the vertical space between each set of lines when there are multiple input sources. The -v (i.e., verbose) option causes tail to print the file name even if there is just a single input file.

Tail could be viewed as a counterpart of the head command, which always starts reading from the beginning of files and which can continue until any specified distance from the beginning. However, there are a few differences. Perhaps the most useful of these is that tail is somewhat more flexible in that, in addition to being able to start reading any specified distance from the end of a file, it can also start at any specified distance from the beginning of a file.

Tail can be instructed to begin printing from some number of lines or bytes from the start of a file by preceding the number with a plus sign instead of a minus sign. For example, the following would print each of the designated files to the display monitor beginning with the seventh line and until the end:

tail +7 aardvark anteater armadillo

The c option could be used to tell tail to print each of the designated files beginning with the seventh byte instead of the seventh line:

tail +7c aardvark anteater armadillo

freshmeat.net Project details for inotail

inotail is a replacement for the 'tail' program found in the base installation of every Linux/Unix system. It makes use of the inotify infrastructure in recent versions of the Linux kernel to speed up tailing files in the follow mode (the '-f' option). Standard tail polls the file every second by default, while inotail listens to special events sent by the kernel through the inotify API to determine whether a file needs to be reread.

TTTT head and tail

Similarly, tail shows the last few lines (again, 10 by default) of a file or stream:
% ypcat passwd | tail
cal:*:1492:160:Calvin Hobbes:/home/cal:/bin/csh
adams:*:116:100:John Adams:/home/pkduck-b/adams:/bin/csh
gary:*:1177:20:Gary North:/home/pkduck-e/gary:/bin/csh
quincy:*:1092:20:Quincy Lizard:/home/triton-a/quincy:/bin/csh
talbot:*:1679:75:Bob Talbot:/home/enterprise-a/cadmec/talbot:/bin/csh
help:*:1034:31:Help Desk:/home/lewey-a/processor/help:/bin/csh
glen:*:1543:20:Glen Carpenter:/home/pkduck-e/glen:/bin/csh 
brent:*:1799:706:Brent Adams:/home/dogbert-a/brent:/bin/csh
lou:*:1701:30:Lou Grant:/home/pkduck-h/lou:/bin/csh 
adam:*:1124:317:Adam Baker:/home/lewey-d/adam:/bin/csh

As with head, you can specify the number of lines from the end of the file at which to start display by prefacing the number with a hyphen ("-"). You can also specify the location as a number of lines from the beginning by prefacing the number with a plus sign ("+"):
% wc -l /usr/man/man1/tail.1
     143 /usr/man/man1/tail.1
% tail +141 /usr/man/man1/tail.1
.LP
Various kinds of anomalous behavior may happen with character special
files.
% tail -3 /usr/man/man1/tail.1
.LP
Various kinds of anomalous behavior may happen with character special
files.

Since the tail man page has 143 lines, "tail +141" is equivalent to "tail -3" in this case.

Unlike with head, tail will operate on units other than lines. By adding the character "b" to the number, you can display the last blocks, and with "c" the last characters:


% tail -10c /usr/man/man1/tail.1
al
files.

Don't forget about newlines.

tail also has the unexpected function that it allows you to reverse the lines spewed. By default, with just the "-r" flag, tail displays the whole file in reverse order by line:


% cat > foo << END
this
is
a
test
END
% tail -r foo
test
a
is
this

A number of lines to reverse can be included, but it cannot be relative to the beginning of the file, only the end. ("+" acts just like "-")

tail also has the wildly useful feature that it can be told to wait around for further input to a file rather than stopping when it gets to the end. This is especially useful to monitor the progress of a process whose output you have redirected to a file:


% clearmake >& Transcript &
[1] 12688
% tail -f Transcript
[the output from the clearmake will be displayed here as it arrives in "Transcript"]
When called in this manner, the tail will continue to read lines from the file and wait for more until you kill it ("^C" is handy here ;-) even after the clearmake has completed and there is no longer output being appended to the file.

One final difference between head and tail: while head will operate on multiple files, tail will not, so don't expect something like "tail foo bar biff" to show anything more than the last 10 lines of "foo".