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
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
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
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
Combines (concatenates) multiple documents into one document. Can be used on individual files as well.
Also try using nl to number lines (it can do more complex numbering), you will find it under under this section, the Section called Text manipulation tools
Example:
cat filepart1 filepart2 filepart3 > wholefile.txt
This will combine (concatenate) filepart1, filepart2 and filepart3 into the single file “wholefile.txt”.
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.
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, the Section called Compression in Chapter 15.
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, the Section called Compression in Chapter 15.