11.2. Text Viewing Tools

head

With no options it shows the first ten lines of a text file.

Use head -n x (where “x” is a number) to display the first x lines.

Try head -F to use a continually updated version of head (if the file changes it will be reloaded and displayed), please note that using this option will run head is a continuous loop so you'll need to use CTRL-C to exit.

For example:

head -n 20 somelog.txt 

Will display the top 20 entries of the file “somelog.txt”.

tail

With no options it shows the last ten lines of a file.

Use tail -n x (where “x” is a number) to display the last x lines.

Try tail -F to use a continually updated version of tail (if the file changes it will be reloaded and displayed), please note that using this option will run tail is a continuous loop so you'll need to use CTRL-C to exit.

For example:

tail -n 20 somelog.txt

Will display the last 20 entries of the file “somelog.txt”.

less

Views text, can scroll backwards and forwards. Has many different options which are all described in the manual page.

When less is already running, use :n and :p (type a colon then the character) to move to the next and previous files (when there are multiple open files).

Command syntax:

less filename.txt

Or using a tool (in this example cat):

cat file.txt | less
more

Displays text, one page full at a time, more limited than less. In this case less is better than more.

more filename.txt

Or using a tool (is this example cat):

cat file.txt | more
cat

Combines (concatenates) multiple documents into one document. Can be used on individual files as well.

Some useful options:

  • -b --- number all non-blank lines

  • -n --- number all lines.

Also try using nl to number lines (it can do more complex numbering), you will find it under this section, Section 11.4

Example:

cat filepart1 filepart2 filepart3 > wholefile.txt

This will combine (concatenate) filepart1, filepart2 and filepart3 into the single file “wholefile.txt”.

tac

Combines (concatenates) multiple documents into one document and outputs them in reverse order. Can also be used on individual files. Notice that tac is cat written backwards.

Example:

tac filepart1 filepart2 filepart3 > wholefile.txt

This will combine (concatenate) filepart1, filepart2 and filepart3 into the single file but have each of the files written in reverse.

z* commands

Many commands can be prefixed with a “z” to read/work within a gzip compressed file.

Some examples are zcat, zless, zmore, zgrep, zcmp, zdiff.

There are many utilities for working with text within compressed files without trying to manually de-compress them somewhere first...most begin with a “z”. You will find some of them mentioned over here, Section 15.3.

bz* commands

There are also a few commands that prefixed with a “bz” to read/work within a file compressed with bzip2.

The tools are bzcat, bzless, bzgrep. You will find some of them mentioned over here, Section 15.3.