Head command in Linux / Unix

In this tutorial, we will learn the basic use of head command in Linux / Unix system.
Head command is widely used for System Administration. As the name suggest, it shows the head part means show output of the first part of files .

Introduction Of Head Command

Head command in Linux / Unix system, show output of the first part of files. By-default the head command print first 10 lines of the files.

How to use head command

Without using any option with head command. It print the first 10 lines of the file.

Syntax:

head option filename

See below given example. Here, we will not use any option and by-default tail command will print first 10 lines of file

linux@tuxworld:/partition9$ tail changelog.txt 
- flash standalone update to 11.2.202.251
- flash mozilla plugins update to 11.2.202.251
- update kernel 3.2.0-34
- firefox 17.0.1
- thunderbird 17.0
- seamonkey 2.14.1
- all system packages updated to their latest stable release

After disk installation, select your own language and keyboard with
language selector tool.
linux@tuxworld:/partition9$ 

To print first n number of lines

To print first n number of lines from file. We generally use -n option.

For example, we would like to print first 4 number of lines from file.
Here, changelog.txt is file name.

linux@tuxworld:/partition9$ head -n 4 changelog.txt 
mozillux v12.12.1
- fixed a bug on java7, preventing the update
- java7 update to latest stable release with new setting tools
- fixed a bug on resonant/mumble server startup
linux@tuxworld:/partition9$

You can also print n number of line by only using -(hyphen) instead of -n option. With hyphen also write the desire number of lines you would like to print.

For eg. Printing first 6 lines from file

linux@tuxworld:/partition9$ head -6 changelog.txt 
mozillux v12.12.1
- fixed a bug on java7, preventing the update
- java7 update to latest stable release with new setting tools
- fixed a bug on resonant/mumble server startup
- fixed locale bug, live-dvd now default to english
- added drum machine and sound mixer
linux@tuxworld:/partition9$

Others Options available with Head Command

The below given options are available with Head Command. We have taken the reference from Man Pages –

head command

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.