Skip to Content
Linux Shell Scripting Cookbook - Third Edition
book

Linux Shell Scripting Cookbook - Third Edition

by Clif Flynt, Sarath Lakshman, Shantanu Tushar
May 2017
Beginner
552 pages
28h 47m
English
Packt Publishing
Content preview from Linux Shell Scripting Cookbook - Third Edition

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
        1
        2
        3
        4
        5
        6

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Linux Shell Scripting - Second Edition

Mastering Linux Shell Scripting - Second Edition

Mokhtar Ebrahim, Andrew Mallett

Publisher Resources

ISBN: 9781785881985