How to do it...

The head command reads the beginning of the input file.

  1. Print the first 10 lines:
        $ head file
  1. Read the data from stdin:
        $ cat text | head
  1. Specify the number of first lines to be printed:
        $ head -n 4 file

This command prints the first four lines.

  1. Print all lines excluding the last M lines:
        $ head -n -M file

Note that it is negative M.

 For example, to print all the lines except the last five lines, use the following command line:

        $ seq 11 | head -n -5

This command prints lines 1 to 5:

      $ seq 100 | head -n 5
  1. 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.
  2. To print the last ...

Get Linux Shell Scripting Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.