O'Reilly logo

Linux Shell Scripting Essentials by Sinny Kumari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filtering based on lines—head and tail

To display the content of a file, we will use the cat command. The cat command displays the whole file content on stdout. However, sometimes, we are interested in viewing only a few lines of a file. In this case, using cat will be tedious because we will have to scroll down to particular lines that we are interested in.

Shell provides us the head and tail commands to print only the lines in which we are interested in. The main difference between both the commands is, head prints the lines from the beginning of the files, and tail prints the lines from the end of the files.

Printing lines using head

The syntax is as follows:

head [OPTION] [FILE …]

By default, head prints first 10 lines of each FILE to stdout ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required