Tuesday Tiny Techie Tip

head(1) and tail(1)

Sometimes you don't want to look at a whole file or all the output from a command, but just the first or last few lines of it.

head and tail provide a means to this end. Unfortunately, they seem to have been written by different people with different ideas about what would be useful, so they work a little differently.

head

head is used to show the first few lines of a file or stream. By default it shows 10 lines:
% ypcat passwd | head
bsmith:*:1243:317:Bob Smith:/home/baker-a/bsmith:/bin/csh
wayne:*:182:60:Wayne Simpson:/home/wayne:/bin/csh
walker:*:1791:100:Lisa Walker:/home/dogbert-a/walker:/bin/csh 
jim:*:1644:100:Jim Smith:/home/dogbert-a/jim:/bin/csh
paul:*:1724:135:Paul Jones:/home/dilbert-a/paul:/bin/csh 
harry:*:1611:100:Harry Palmer:/home/pkduck-f/harry:/bin/csh
alex:*:1959:100:Alex Watson:/home/alex:/bin/csh 
sue:*:1988:100:Sue Foo:/home/sue:/bin/csh
marty:*:1318:78:Marty Taylor:/home/manfac-a/home/marty:/bin/csh

You can specify a different number of lines by prefacing the number with a hyphen ("-"):
% ypcat passwd | head -2
bsmith:*:1243:317:Bob Smith:/home/baker-a/bsmith:/bin/csh
wayne:*:182:60:Wayne Simpson:/home/wayne:/bin/csh

If you specify more than one filename on the commandline, head will show the first few lines of each file with a one-line banner identifying each file:
% head -2 /usr/man/man1/head.1 /usr/man/man1/tail.1
==> /usr/man/man1/head.1 <==
.\" @(#)head.1 1.17 90/02/15 SMI; from UCB 4.1
.TH HEAD 1 "9 September 1987"

==> /usr/man/man1/tail.1 <==
.\" @(#)tail.1 1.14 90/02/15 SMI; from S5R2 6.2 83/09/02
.TH TAIL 1 "14 August 1989"

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".


Tuesday Tiny Techie Tip -- 22 April 1997
Forward to (05/06/97)
Back to (04/22/97)
Written by Jeff Youngstrom

Up to the TTTT index

Tuesday Tiny Techie Tips are all © Copyright 1996-1997 by Jeff Youngstrom. Please ask permission before reproducing any of this material.