The head command reads the beginning of the input file.
- Print the first 10 lines:
$ head file
- Read the data from stdin:
$ cat text | head
- Specify the number of first lines to be printed:
$ head -n 4 file
This command prints the first four lines.
- Print all lines excluding the last M lines:
$ head -n -M file
For example, to print all the lines except the last five lines, use the following command line:
$ seq 11 | head -n -5 1 2 3 4 5 6
This command prints lines 1 to 5:
$ seq 100 | head -n 5
- Printing everything except the last lines is a common use for head. When examining log files we most often want to view the most recent (that is, the last) lines.
- To print the last ...