Sunday, July 25, 2021
HomeA-Z CommandsLinux Head command Tutorials and Examples for Beginners

Linux Head command Tutorials and Examples for Beginners

The Linux head command is a simple command-line utility that allows users to display the first few portions of a file. Most people use it for viewing the topmost part of configuration files. But you can also use it for inspecting any files. In this guide, we have illustrated a few examples of how to use the head command.

By the end of this guide, you should be able to learn its proper usage. Once you do so, you will become much fluent at navigating files directly from the terminal. So, continue reading to find out more about the head command in Linux.

Useful Example of the Linux Head Command


The head command is one of the most simple Linux terminal commands. Thus, it is very easy to master this command and use it in day to day computing tasks. Check out the following examples to learn how the head command works.

1. Print the First Ten Lines of a File


By default, the head command prints the first ten lines of the specified file. Run the below command in your favorite Linux terminal emulator to see how it works in practice.

$ head /usr/share/dict/american-english

This command will display the first ten lines of the /usr/share/dict/american-english file. Note that we will use this file throughout this guide since it is readily available on most Linux distributions.

Linux head command

2. Print the First N Lines of a File


We can specify the number of lines we want to view using the head command. To do this, simply add the -n flag followed by the number of lines. For example, the below command displays the first 15 lines from the american-english dictionary of Ubuntu.

$ head -n 15 /usr/share/dict/american-english

You can also use the long-style syntax –lines instead of -n. Check out the below example to see how it works.

$ head --lines 15 /usr/share/dict/american-english

3. Print the First N Bytes of a File


You can use the head command to print the first N characters of a file. Simply use the -c option followed by the number of characters you want to display. The below command demonstrates this using our example file.

$ head -c 30 /usr/share/dict/american-english

This command will print the first 30 bytes from the /usr/share/dict/american-english file. The long-form alternative for -c is –bytes.

$ head --bytes 30 /usr/share/dict/american-english

Note that this option also counts newlines(\n) as a single character. So you may need to tweak around a bit to display the required characters.

print bytes using head command

4. Print Lines From Multiple Files


You can use the Linux head command to display the first parts from multiple files. Simply enter the file names one after another, each separated by a space. Check out the below examples to see how it works.

$ head -n 6 /usr/share/dict/american-english /usr/share/dict/british-english

This command will display the first 6 lines from the american-english and british-english files. The output is separated by a header that indicates which file the lines belong to.

5. Disable File Headers in Output


If you do not want to display the file header, you can disable it using the -q option. Check out the below example to see how this works in Linux.

$ head -n 6 -q /usr/share/dict/american-english /usr/share/dict/british-english

When you run the above command, it will print the first six lines from both files. However, the output will not indicate which file the lines belong to. The alternatives to the -q option are –quiet and –silent.

$ head --lines 6 --quiet /usr/share/dict/american-english /usr/share/dict/british-english
$ head --lines 6 --silent /usr/share/dict/american-english /usr/share/dict/british-english

6. Always Print File Headers


If you want to make sure that the file headers are always printed, you can use the -v flag. It can be useful when working with sensitive documents that require further cross-checks. Check out the below example to see it in practice.

$ head -n 6 -v /usr/share/dict/american-english

If you omit the -v flag and run the command again, you will see no header file present in the output. Anyway, you can also use the long-form syntax –verbose instead of -v, as illustrated by the below example.

$ head --lines 6 --verbose /usr/share/dict/american-english

print file headers for head command

7. Print All Lines From a File Except the Last N Lines


The Linux head command allows users to print all but the last N number lines from each file. To do this, use a ‘-‘ sign before the line number. The following command demonstrates this using a simple example.

$ head -n -6 /usr/share/dict/american-english
$ head --lines -6 /usr/share/dict/american-english

The above commands will print all but the last six lines from the american-english dictionary file. It also works the same way for characters. For example, the below commands will print all but the last 60 characters from the example file.

$ head -c -60 /usr/share/dict/american-english
$ head --bytes -60 /usr/share/dict/american-english

8. Set NUL as the Line Delimiter


The head utility’s  -z option allows users to set the NUL(\0) character as a line delimiter instead of the newline character. This can be helpful when the lines are separated using spaces rather than newlines. Check out the below example to see how this works.

$ head -c 6 -z /usr/share/dict/american-english

The long-form syntax for the -z option is –zero-terminated. The below example uses this syntax.

$ head --bytes 6 --zero-terminated /usr/share/dict/american-english

9. Display the Help Page


The help page of Linux commands display summarized information of all the available options and how to use them. You can view this page for the head command by using the following simple command.

$ head --help

It will print out the options available to you and the syntax of head commands. This option does not have a short form.

10. Display the Man Page


The man page or manual or a Linux command provides in-depth information regarding the command. It is the best place to find information on command or its options. You can view the man page for the Linux head command by using the below command.

$ man head

Consult this page whenever you need detailed information about the head command’s usage.

Ending Thoughts


The head command is a simple but useful utility for file manipulation. If you know how to use it effectively, your everyday work will become much simpler. Luckily, it is a very simple tool and only has a few command-line options. This makes it easy to master this command in no time.

If you try out the commands shown in this guide, you should be well able to use them effectively in the future. You can also bookmark this guide for future references if you want. Hopefully, we are successful in providing you the information you were looking for. Leave us a comment below if you have any further questions regarding this tool.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

spot_img

Latest Post

Must Read