Use of Head Command in Linux - With different examples ?

The head command is a command-line utility for outputting the first part of files given to it via standard input. It writes results to standard output.

By default head returns the first ten lines of each file that it is given.

Basically, the head command is a command-line utility through which you can easily retrieve the top data from a specified file and show the result to standard output.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux Commands related queries.

In this context, we shall look into how to use the head command, a command-line utility on a Linux system. We will explain head command options with brief examples.


What is the Syntax of Head Command ?

The head command has the following syntax:

$ head <options> <filename>

i. Options: Head options are used to specify the action which action performs on a file. For example, use option -n to specify the line number.

ii. Filename: you can specify one or more file names as input using this argument. If you will not mention a file name then, it reads the standard raw input.


How to Use head command with examples ?

In this guide, you will learn different uses of head command.


1. Use of Head command without an option

When the head command is used with file name without any option then, in that case, it returns the first 10 lines of a given file as follows:

$ head filename.txt

For example, using the head command we want to retrieve the first 10 lines from our test file:

$ head filename.txt


2. Print the specified lines using the head command

By using the -n (–lines) option along with the specified integer number, you can display the desired number of lines from a file as follows:

$ head -n <integer> filename.txt

For example, you want to print the first 20 lines from a file. In this case, you would mention 20 with option n as follows:

$ head -n 20 filename.txt

You can also omit the option n from the above command and just mention the integer with a hyphen as follows:

$ head -20 filename.txt


3. Print specified bytes using the head command

You can also print the specified number of bytes of a file by using the option ‘-c’ along with the head command as follows:

$ head -c <integer> filename.txt

Let's explain with an example, to print the first 200 bytes of data from a file through the head command. 

You would run the following command:

$ head -c 200 filename.txt

The multiplier suffix can also specify to display the bytes. For example,

The following multipliers you can use with a specified integer of bytes:

i. b multiplies by 512.

ii. kB multiplied by 1000

iii. K multiplied by 1024

iv. MB multiplied by 1000000

v. M multiplies by 1048576


To print the top 2 kilobytes, execute the following command:

$ head -c 2k filename.txt


4. Specify multiple files as input with head command

The multiple files, you can also take as input with the head command as follows:

$ head testfile.txt testfile2.txt

The above-mentioned command will show you the top ten lines from each file name.

The same options are used with the head command in case of multiple files.


5. Head command use with the tail command

The head command can be also used with other commands. 

For example, to print the lines between 10 and 20 lines, you can use the tail command as follows:

$ head -n 20 testfile.txt | tail -10


6. Head command pipeline with other commands

The head command can also be used as a pipe with other commands. 

For example, to display the most recent 2 modified files use the following command:

$ ls -t /etc | head -n 2


[Need urgent assistance in fixing Linux related errors? We can help you. ]

This article covers how to use head command with all required options. 

By using the tail command with a head command, you can also display the last lines of a file on the terminal.

The head command, as the name implies, print the top N number of data of the given input. 

By default, it prints the first 10 lines of the specified files. 

If more than one file name is provided then data from each file is preceded by its file name. 


Head command Syntax:

head [OPTION]... [FILE]...

Related Posts