head Command in Linux

Back

The head command reads the first few lines of any text given to it as an input and writes them to standard output (which, by default, is the display screen).

Syntax :

head [options] [file(s)]
The square brackets indicate that the enclosed items are optional. By default, head returns the first ten lines of each file name that is provided to it.

For example: 

The following will display the first ten lines of the file named aardvark in the current directory (i.e., the directory in which the user is currently working):
    $ head aardvark

If it is desired to obtain some number of lines other than the default ten, the -n option can be used followed by an integer indicating the number of lines desired. For example, the above example could be modified to display the first 15 lines from each file:
    $ head -n15 aardvark armadillo

head can also return any desired number of bytes (i.e., a sequence of eight bits and usually long enough to represent a single character) from the start of each file rather than a desired number of lines. 

This is accomplished using the -c option followed by the number of bytes desired. For example, the following would display the first five bytes of each of the two files provided:
    $ head -c 5 aardvark anteater