From the name implies, the head command shows by default the 10 top lines from a file. This command can be piped with other commands to produce standard output. 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.
We will explain in this tutorial, how to use the head command, a command-line utility on a Linux system. We will explain head command options with brief examples.
Syntax of Head Command
The head command has the following syntax:
head <options> <filename>
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.
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.
Use of head command with examples
We will discuss the following different uses of head command in this article:
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 testfile.txt
The following output returns on the terminal:
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 testfile.txt
You can also omit the option n from the above command and just mention the integer with a hyphen as follows:
$ head -20 testfile.txt
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 testfile.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:
- b multiplies by 512.
- kB multiplied by 1000
- K multiplied by 1024
- MB multiplied by 1000000
- M multiplies by 1048576
To print the top 2 kilobytes, execute the following command:
$ head -c 2k testfile.txt
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.
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
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
We have given in this article a good understanding of the use of head command with all required options. We have seen how to use other commands with head commands effectively. By using the tail command with a head command, you can also display the last lines of a file on the terminal.