Head and tail

For testing the next few commands, we will need a file with a sequence of numbers from 1 to 100. For this, use the following command:

    $ seq 100 > numbers.txt

The preceding command creates a file with the numbers 1 to 100 on separate lines. The following example shows the usage of the head command:

    $ head numbers.txt      // will display 10 lines
    $ head -3 numbers.txt   // will show first 3 lines
    $ head +5 numbers.txt   // will show from line 5. In few shells this command may not work

The following example shows the usage of the tail command:

    $ tail numbers.txt      // will display last 10 lines
    $ tail -5  numbers.txt        // will show last 5 lines
    $ tail +15 numbers.txt  // will show from line 15 onwards. In few shells this may not work

To print ...

Get Learning Linux Shell Scripting - Second 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.