This article will explain to you
- How to read data using head command?
- How to use head arguments?
- Combination of cat or another command with head
And a detailed explanation of head command usage.
Table of Contents
What is HEAD?
A head command is part of GNU Coreutils, which is used to print few lines of content on STDOUT and by default, it will print initial 10 lines of file content.
Head Command Syntax:
The basic syntax for using head command is as follows:
head [OPTION]... [FILE]...
- [OPTION] It is optional to use; some of the options will discuss later.
- [FILE] To read file content, you need to provide input using STDIN or PIPE (|)
How to use the HEAD command
Let see the first use case of head command without any options. To perform this, we have shortlisted 50 Linux Distributions names from Distro watch.
$ head linux_distro.txt 1 MX Linux 2 Manjaro 3 Mint 4 Pop!_OS 5 EndeavourOS 6 Ubuntu 7 Debian 8 elementary 9 Fedora 10 openSUSE
You can see the above command print 10 lines of content from linux_distro.txt despite showing all the content once.
Read specific number of line
On top, you have just seen head command print only top 10 file content, then what is the use of head command when it just read top 10 content of file what about the next lines, how would I read that? Calm, my pal, I’ll show you that too.
For example, I need to print file content till line number 25, To preform this will use -n or (–lines) options and assign number:
$ head -n 25 linux_distro.txt 1 MX Linux 2 Manjaro 3 Mint 4 Pop!_OS 5 EndeavourOS 6 Ubuntu 7 Debian 8 elementary 9 Fedora 10 openSUSE 11 KDE neon 12 Solus 13 Zorin 14 Garuda 15 deepin 16 Arch 17 antiX 18 Linuxfx 19 Puppy 20 Ubuntu Kylin 21 CentOS 22 Kali 23 PCLinuxOS 24 Mageia 25 ArcoLinux
The above command print 25 lines to a terminal, even you can put -n or –lines after declaring file name:
$ head linux_distro.txt --lines 25
This will also show the same output as above. That’s why I’m not pasting work again. I’ll prefer to use the first option to avoid errors.
The head command can work even without specifying -n or –lines. Simply type – and mention number:
$ head -15 linux_distro.txt
Read specific number of Bytes
It is also a pretty good option to read a specific number of Bytes or, say, a single character using -c or–bytes.
For better understanding, we will print 100 bytes or 100 characters from linux_distro.txt. To print, pass the following command:
$ head -c 100 linux_distro.txt 1 MX Linux 2 Manjaro 3 Mint 4 Pop!_OS 5 EndeavourOS 6 Ubuntu 7
Don’t forget whitespace or \n also count as a character.
Well, you can perform this step more sophistically. Instead of putting bytes like 1000, you can simply use multiplier suffix. For example, 1000 bytes convert into Kilobyte? Todo simply divides with 1000. Consequently answer will be 1kb (kB stand for Kilobyte).
$ head -c 1kB linux_distro.txt
Read multiple file using head
Sometimes I use the head command to compare between files. You are thinking how? Let see, to read multiple, you just need to mention the file name that’s all head command will automatically print the file content.
$ head linux_distro.txt demo_file.txt ==> linux_distro.txt <== 1 MX Linux 2 Manjaro 3 Mint 4 Pop!_OS 5 EndeavourOS 6 Ubuntu 7 Debian 8 elementary 9 Fedora 10 openSUSE ==> demo_file.txt <== Alpha Beta Gamma
Intentionally I have not specified the number of lines. If you want, you can do it freely. As you can see, every file has preceded with a file name.
Although it’s pretty convenient to read data like but you want all data should merge and print as a single entity, type the following command:
$ head -q linux_distro.txt demo_file.txt 1 MX Linux 2 Manjaro 3 Mint 4 Pop!_OS 5 EndeavourOS 6 Ubuntu 7 Debian 8 elementary 9 Fedora 10 openSUSE Alpha Beta Gamma
Not enough! You want to read all the file once which is present in current directory pass the following command:
$ head -n 1 * ==> demo_file.txt <== Alpha ==> file1 <== demo_file.txt ==> linux_distro.txt <== 1 MX Linux
In the above command, we have used -n options and * wild card to get all the input files and print line number 1 content.
Head use with pipline
To leverage the power of head command, you can use pipeline | along with the head for a complex operation.
Whenever you read the manual, it’s print the full content. If you want to read a specific number of the line, just pass the below command:
$ man head | head -n 10 HEAD(1) User Commands HEAD(1) NAME head - output the first part of files SYNOPSIS head [OPTION]... [FILE]... DESCRIPTION Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
The head command is not limited to man command or pipeline, it can perform much more than that.
There is much to know about more commands. If you’re eager to learn more, go to the man page or type man head or info head in a terminal.
Now that you are familiar with the head command, we will drop a guided article on tail command in the upcoming days.
If you want to share something about the head command that I missed or know something that would help others, please share that in a comment section.
If you want to say Hi to me or want to suggest something feel free to contact me.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.