Head Command Examples in Linux (The Complete Guide)

Linux has several commands that helps to display the contents of a file. such as:

  • cat command
  • nl command

Suggested Read: cat Command in Linux with Examples

But these commands prints the entire contents of the file.

If the content of the file is too long, we have to read the whole file to see what we required.

And if there is a command that shows us as much content as we need, it becomes easier to work.

For Example, when we check the log of any Server or application, and we have to review some of the opening lines, it makes no sense for us to display the entire contents.

In such a Scenario, the head command can be used.

What is the use of Head Command?

Head command helps to display the first part of the files.

By default, it prints the first ten lines of a file, but you can display as many lines as you want using its other features.

In this article, I will teach you about the complete features of head command.

First of all, let’s focus on some of the most important options that we can use with the head command.

OptionsExplanation
-n --lines=[-]NUMprint the first NUM lines instead of the first 10
-c --bytes=[-]NUMprint the first NUM bytes of each file
-qnever print headers giving file names
-valways print headers giving file names
--help

man
Display Help/Manual page of Head Command
--versionCheck the version of Head command

Syntax:

You must follow the syntax given below to use the head command.

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

1. Display first ten lines of a File

Here I have a file named file.txt which contains some content.

Let’s try to understand the concept of the head command using this file.

~$ cat file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
line 21
line 22
line 23
line 24
line 25

By default the head command prints the first 10 lines of the file without any option.

~$ head file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

2. Display content of Multiple files

You can display the contents of several files. To do this, pass the path of files to head command separated by space.

Refer to the following example. Here I am listing the contents of file.txt and file1.txt

~$ head file.txt file1.txt 
==> file.txt <==
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

==> file1.txt <==
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
List content of Multiple Files

3. Print specified Lines

You can print as many lines as you want instead of the first ten lines.

For that, you have to pass the -n option to head command.

Syntax:

head -n [Number] [Filename]
  • [Number] – Specify the number of lines you want to print.
  • [Filename] – Specify the Filename

Let’s take some examples.

Ex. # 1 – List first 7 lines.

~$ head -n 7 file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7

Ex. # 2 – List first 3 lines.

~$ head -n 3 file.txt 
line 1
line 2
line 3

You can also use the long option --lines. Here is an example:

Ex. # 3 – List first 5 lines (Using long option).

~$ head --lines=5 file.txt 
line 1
line 2
line 3
line 4
line 5

Bonus Tip:

You can avoid using the -n option with the head command.

Example:

~$ head -5 file.txt

4. Print specified Bytes

You can print content according to specified initial bytes instead of lines.

head -c [Number] [Filename]
  • [Number] – Specify the number of Bytes you want to print.
  • [Filename] – Specify the Filename

[Number] may have the following multiplier suffix:

UnitNameCalculations
bblocks512
KBKiloBytes1000
KKibiBytes1024
MBMegaBytes1000*1000
MMebiBytes1024*1024
GBGigaBytes1000*1000*1000
GGibiBytes1024*1024*1024

Let’s take some examples.

Ex. # 1 – List first 50 bytes of the file.

~$ head -c 50 file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7

Ex. # 2 – List first 100 bytes of the file.

~$ head -c 100 file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13

You can also use the long option --bytes. Here is an example:

Ex. # 3 – List first 80 bytes of the file (Using long option).

~$ head --bytes=80 file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11

Let’s take some examples using Multiplier Suffix.

Ex. # 4– List first 50 KiloBytes (KB) of the file.

~$ head -c 50KB file.txt

Ex. # 5– List first 70 MebiBytes (M) of the file.

~$ head -c 70M file.txt

Ex. # 6– List first 1 GigaBytes (GB) of the file.

~$ head -c 1G file.txt

5. Combine Head Command with other Commands

You can use the head command with other Linux commands with the help of Pipe (|).

Let’s take some examples:

Ex. # 1 – Combine head with cat command

Task #1: Print the initial 5 lines of the file using the cat command.

Note: In Linux, the cat command is used to list the contents of a file.

Answer:

~$ cat file.txt | head -n 5
line 1
line 2
line 3
line 4
line 5

Ex. # 2 – Combine head with ls command

Task #1: List the 3 largest files using the ls command.

Note: In Linux, you can list the contents of a directory using the ls command.

Answer:

Here I have a directory called /data that contains some content.

~/data$ ls -lh
total 48K
-rw-r--r-- 1 ubuntu ubuntu 1.8K Jun 12 00:05 file1.txt
-rw-r--r-- 1 root   root   2.4K Jun 12 00:04 file2.txt
-rw-r--r-- 1 ubuntu ubuntu 5.3K Jun 12 00:06 file3.txt
-rw-r--r-- 1 ubuntu ubuntu  191 Jun 12 00:12 file4.txt
-rw-r--r-- 1 ubuntu ubuntu 8.8K Jun 12 00:12 file5.txt
-rw-r--r-- 1 ubuntu ubuntu  339 Jun 12 00:12 file6.txt
-rw-r--r-- 1 ubuntu ubuntu 7.7K Jun 12 00:15 file7.txt
-rw-r--r-- 1 ubuntu ubuntu 2.2K Jun 12 00:15 file8.txt

Refer to the following command to list 3 largest files.

~/data$ ls -S | head -n 3
file5.txt
file7.txt
file3.txt

Task #2: Filter out the latest modified file

Answer:

Refer to the following command to list the latest modified file.

~$ ls -t | head -1

6. Display headers giving file names

You can display the filename as a header when listing the contents.

To do so, pass the -v option to head.

~$ head -v file.txt 
==> file.txt <==
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
Display Filename as a Header

OR use the long option --verbose.

~$ head --verbose file.txt

7. Never display headers giving file names

You can always choose to quiet the Header using -q option. Refer to the following example.

~$ head -q file.txt 
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

OR use the long options --quiet / --silent

~$ head --quiet file.txt
~$ head --silent file.txt

8. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of head command.

~$ head --help
~$ man head

9. Check the version of head command

Check the head command version using the following command.

~$ head --version

Infographic

Refer to this Infographic for complete head command options.

Head command Infographic

You can visit at following websites to get more information on head command.

Conclusion

I hope you have learned something from this article.

I have tried my best to include all the features of head command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment

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