In Linux system, we have some alternatives about how to display a content of text files. We can use cat command, more command or less command. Another command to do it is head command.
Head is command that will print only the first part of a text file. So when you know you need only the first part of the file, head will help you. By default, it will display the first 10 lines.
To use head command is quite easy. We just need to type :
For example, we have a file with 15 lines inside it.
When we run head command, then the result will be like the screenshot below.
We already know that head by default will print the first 10 lines. But we can override this setting using -noption followed by the number of line. For example, if we want to display the first 5 lines, then the command will be like this :
We can also view the content based on file size. For example, the doc_10.txt file has a size of 447 bytes. If we want to know the content of the first 100 bytes, we can use -c option followed by the size. Here’s an example.
You may need this information if you copy the result into another file. Here’s a sample of the header of a file named doc_10.txt
To display multiple files, you can put the files separated by space. Here’s a sample.
This command is also one of the basic command in Linux system. Once again, if you are only need the first part of the file, head may the right tool for you. As usual, you can alway type man head or head –help to display its manual page to explore more detail.
What is head?
Head prints the first N number of data of the given input. By default, it prints first 10 lines of each given file.
5 head Examples
1. Print the first N number of lines
To view the first N number of lines, pass the file name as an argument with -n option as shown below.
$ head -n 5 flavours.txt
Note: When you simply pass the file name as an argument to head, it prints out the first 10 lines of the file.
2. Print N number of lines by specifying N with –
You don’t even need to pass the -n option as an argument, simply specify the N number of lines followed by ‘-‘ as shown below.
$ head -4 flavours.txt
3. Print all but not the last N lines
By placing ‘-‘ in front of the number with -n option, it prints all the lines of each file but not the last N lines as shown below,
$ head -n -5 flavours.txt
4. Print the N number of bytes
You can use the -c option to print the N number of bytes from the initial part of file.
$ head -c 5 flavours.txt
Note : As like -n option, here also you can pass ‘-‘ in front of number to print all bytes but not the last N bytes.
5. Passing Output of Other command to Head Input
You may pass the output of other commands to the head command via pipe as shown below,
$ ls | head
Syntax and Options
head [OPTIONS]… [FILE]…
||to print N bytes from each input file.
||to print N lines from each input file.
||Prevent printing of header information that contains file name
||to print header information always.
7+ useful head command’s switches with example – Unix/Linux
The head command
By default, the head command prints the first 10 lines from the file which is given followed by the head command. In the case of more than one file with head command displays separate output with a header identifying the file name. Head command has a lot of useful and also very helpful switches.
Useful switches with example:
“I’ve copied the ‘/etc/passwd’ and ‘/etc/shadow’ files to pass.txt and sha.txt respectively to demonstrate examples.”
1. head : The head command alone
Which displays the first 10 line by-default.
# head pass.txt
2. head : More than one file
Displays separate output with a header identifying the file name.
# head pass.txt sha.txt
==> pass.txt <==
==> sha.txt <==
3. head -n : Displays last “n” lines from the text file
# head -n 3 pass.txt
4. -c, –bytes=K
Output the first K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file.
# head -c 100 pass.txt
5. -q, –quiet, –silent
Never print headers giving file names.
6. -v, –verbose
Always print headers giving file names.
Display this help and exit.
Output version information and exit.