head, tail and a story about a cat

The next in the series of Basic UNIX commands for Windows users concentrates on the related commands of head, tail and cat.

The first in of these related commands is head, this command is used to display the first 10 lines of an ASCII file. So what good? is that I hear you shout, well let me illuminate you 😀 in the Unix world a lot if useful information is contained in the first few lines of a file. what use is that if the info I need is on line 20, well just add the -20 to it 😀

head -20 my file

this returns the first 20 lines.

head is mainly utilised when coupled with the pipe command “|” for example what follows is a command often used in weblog analysis.

% sort -t" " -k 3 a.txt | head -1

this command sorts the file “a.txt” setting the delimiter to “spaces” (-t) and starting at the third column and pipes the result through to head which displays the first line to the console.

it you want to see the top few line of several files than just write

head -5 file1 file2

this will display the files preceded with a line like

===> filename <===

by default you do not get the above when invoking head against a single file so use

head -n file1 /dev/null

If you want the name of the file displayed despite the fact that its the only one you can use /devnull as the second file or the first file

You may choose to use head to display the entire contents of multiple short files so each file will be preceded by its filename. In this case you need to specify a large enough n. The following command provides an example:

head -99999 file1 file2 file3

So what about head, cousin tail, well, let me tell you about this little tyke 😀

well by default the tail command also prints ten lines of a file, but this time it prints the last 10 lines:  Well that seems a bit more useful, you say.

tail file1

Like the head command, the tail command also lets you specify a number other than 10 using the -n option:

tail -25 file1

Another trick of tall is that numbers having a leading plus (`+’) sign are relative to the beginning of the input, for example, -c +2 starts the display at the second line of the input and -c +2 fro the second byte. for example if you have a file that contain some kind of header in the first line following by data you need (this is often that case in tables where the first line contain names of columns). You can skip the header using tail +2 command:

tail +2 lines
Line 2
Line 3
Line 4
Line 5
... ... ...

Here a number starting with a plus (+) sign is an offset relative to the top of the file. BTW that means that tail +1 file gives you the entire file, the same as cat. +2 skips the first line, and so

Even more useful in some situations, is the ‘-f’ parameter to the ‘tail’ command. This causes tail to ‘follow’ the output of the file. Initially, the response will be the same as for ‘tail’ on its own – the last few lines of the file will be displayed. However, the command does not return to the prompt, and instead, continues to ‘follow’ the file. When additional lines are added to the file, they will be displayed on the terminal. This is very useful for watching log files, or any other file which may be appended over time. Type ‘man tail’ for more details on this and other tail options.

tail -f mylog.log

as well as changing the number of lines printed, the printing units (lines, blocks or bytes) may be changed. These options are:

-b number The location is number 512-byte blocks.
-c number The location is number bytes.
-n number | -number The location is number lines.

-r The -r option causes the input to be displayed in reverse order, by line.

Additionally, this option changes the meaning of the -b, -c, and -n options. When the -r option is specified, these options specify the number of bytes, lines or 512-byte blocks to display, instead of the bytes, lines, or blocks from the beginning or end of the input from which to begin the display. The default for the -r option is to display all of the input.
just like the head command if more than a single file is specified, each file is preceded by a header consisting of the string “==> Filename <==,  you can do the same /dev/null trick to spoof it as well.

The cat command concatenates and displays files to the screen these can be piped to another command to create a primitive editor for one liners or for adding a line to the config file.

as explained about it can number lines so in a way it can serve as an input to another command. for example:

cat -n var/adm/messages | more

You can use it to join two files.  Thus: cat file prints file on your terminal, and:

cat file1 file2 >file3

concatenates file1 and file2, and writes the results in file3. If no input file is given, cat reads from the standard input file. That permits to create small files with cat instead of editor like vi or ed

cat supports the following options:

  • -n Precede each line output with its line number.
  • -b Number the lines, as -n, but omit the line numbers from blank lines.
  • -u The output is not buffered. (The default is buffered output.)
  • -s cat is silent about non-existent files.
  • -v Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly. ASCII control characters (octal 000 – 037) are printed as ^n, where n is the corresponding ASCII character in the range octal 100 – 137 (@, A, B, C, . . ., X, Y, Z, [, \, ], ^, and _); the DEL character (octal 0177) is printed ^?. Other non-printable characters are printed as M-x, where x is the ASCII character specified by the low-order seven bits.
    When used with the -v option, the following options may be used:
  • -e A $ character will be printed at the end of each line (prior to the new-line).
  • -t Tabs will be printed as ^I’s and form feeds to be printed as ^L’s.
    The -e and -t options are ignored if the -v option is not specified.

Edits with cat

So how about a couple of real life uses.  Well the slackers of the world use it as a general pager (cat file) and a complete text-editing environment (cat > file). Its syntax is unrivaled in its simplicity and, for text editing one-liners, it gives you quick ways to append or insert text without an editor.  now that is cool 😀

Using cat to concatenate files and standard input streams

 $ (cat - input1 - input2 - input3 - input4) | mailx ted
Take a look at these example files.
This is the first file ...
This is the second file ...
This is the third file -- note the fourth paragraph below ...
And here's the last file ...

Add text to the end of a file

The slackers are on to something, though. When you need to append text to the end of a file, there’s nothing quicker than cat:

$ cat >> file
 > line
 > line
 > line

While you’re adding lines, pressing Ctrl-U erases the current line, Ctrl-Z suspends the process, and Ctrl-C aborts everything. When you’re done, press Ctrl-D on a line of its own.

If the data you’re entering is an X selection that you’re pasting from another window, this one-liner is generally quicker to use than calling up an editor, opening the target file, moving to the end of the file, pasting the selection, saving the file, and exiting the editor. It can also be more useful when you’re pasting formatted or specially formatted text, and you want to keep the formatting because some text editors and editing modes reformat the X selection when you paste it.

====================Now this is Important=====================

Although this operation is a common, everyday practice, you always have to be careful that you use the shell operator for appending redirection (>>) and not the regular redirection operator (>); if you mistakenly use the latter, you’ll overwrite the contents of the file with the text you mean to append.   Oops better not do that then 🙂


To add the entire contents of one file to the end of another file, give the filename:

$ cat footnotes.txt >> file

If you’re appending only a single line instead of multiple lines or an entire file, you can use echo instead of cat:

$ echo " myhostname" >> /etc/hosts

To append lines of text that are itemized beginning with 1, use cat’s -n option; lines are preceded with the line number (offset with up to five space characters) and a tab character. Add the -b option to suppress the numbering of blank lines:

$ cat -nb > file
 This line is numbered
 And so is this
Another numbered line
 $ cat file
 1 This line is numbered
 2 And so is this?
3 Another numbered line

now this is useful to Insert text at the beginning of a file you use th minus sign (-) and write to a new file:

$ cat - file > newfile
 This is the beginning of the file
 And then the old file is inserted
 Below this line:

Although it’s simple, the disadvantage of this one-liner is that it creates a new file. If you want to insert text into the original file, the renaming shuffle you have to do makes this almost more trouble than it’s worth. Better ways are just ahead with ed.

Show non-printing characters

cat has several useful options. Some of them control the way it outputs non-printing characters, such as tabs and control characters. To determine whether a file or a group of text files has embedded control characters, use these options. For instance, if a file has trailing blanks, you can see them:

$ cat -vet input.txt
This line has trailing blanks. $
 This line does not.$

Well that is our story complete.