How to use the head Command in Linux

In this tutorial, we will be showing you how to use the head command on your Linux or Unix-based system.

head command on Linux

The head command on Linux allows you to easily get the top lines from the specified file or the standard input.

It is the opposite of the tail command. That command is used to get the lines from the bottom of a file.

This tool is handy when you only want the top part of a file, whether this is several lines or a certain number of bytes.

Over the following few sections, we will explore the various ways to utilize the head command within your Linux system.

Table of Contents

Syntax of the head Command on Linux

Below you can see the syntax for the head command on Linux. It has two parameters, both of which can be completely optional.

When used without any options, head will retrieve the first 10 lines of the specified file. If no file is specified, it will expect the data to be passed in through standard input.

head [OPTIONS] [FILE]...

The “[OPTIONS]” parameter will allow you to control how the head tool works. For example, you can use the options parameter to set the number of lines that it will retrieve from the top of a file.

You can use the “[FILE]” parameter to specify any file from which you want to retrieve the top lines. This tool will happily handle as many files as you throw at it.

Basic Usage of Linux’s head Command

Let us start with the most basic usage of the head command within Linux. The simplest way to utilize the head tool is to only pass in files.

By only passing in a file, head will output the top 10 lines from the specified file or files to the standard output.

The head command will output a header for each file if multiple files are specified.

You can see that the syntax for this is to use “head“, followed by the path to any file you want the top lines retrieved from.

head [FILE]...

Example of using Linux’s head Command on a Single File

For our first example, let us show how Linux’s head command works when used on a single file.

We will be using this on our Linux systems syslog file located at “/var/log/syslog“.

head /var/log/syslog

After running this command, you can see that the head tool returned the top 10 lines from the file.

From our example response, you can see that this is from the top as the logs are after the log file was “rotated”.

Apr  3 00:00:11 pimylifeup systemd[1]: rsyslog.service: Sent signal SIGHUP to main process 504 (rsyslogd) on client request.
Apr  3 00:00:13 pimylifeup systemd[1]: logrotate.service: Succeeded.
Apr  3 00:00:13 pimylifeup systemd[1]: Finished Rotate log files.
Apr  3 00:00:14 pimylifeup systemd[1]: man-db.service: Succeeded.
Apr  3 00:00:14 pimylifeup systemd[1]: Finished Daily man-db regeneration.
Apr  3 00:00:15 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:15 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so
Apr  3 00:00:15 pimylifeup brltty[1065]: screen driver not loadable: a2
Apr  3 00:00:20 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:20 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so

Example of Using head On Multiple Files

Now that we have used head on a single file let us show how the command handles it when you throw multiple files at it.

To use multiple files with Linux’s head command, you must specify one file after another. Make sure to separate them by using a single space.

For this example, let us use “head” on “/var/log/syslog” and “/var/log/boot.log“.

head /var/log/syslog /var/log/boot.log

Below you can see how the head command handles multiple files. The contents of each file were output to the standard output.

For each file the command processes, it separates the retrieved lines by outputting a header.

==> /var/log/syslog <==
Apr  3 00:00:11 pimylifeup systemd[1]: rsyslog.service: Sent signal SIGHUP to main process 504 (rsyslogd) on client request.
Apr  3 00:00:13 pimylifeup systemd[1]: logrotate.service: Succeeded.
Apr  3 00:00:13 pimylifeup systemd[1]: Finished Rotate log files.
Apr  3 00:00:14 pimylifeup systemd[1]: man-db.service: Succeeded.
Apr  3 00:00:14 pimylifeup systemd[1]: Finished Daily man-db regeneration.
Apr  3 00:00:15 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:15 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so
Apr  3 00:00:15 pimylifeup brltty[1065]: screen driver not loadable: a2
Apr  3 00:00:20 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:20 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so

==> /var/log/boot.log <==
------------ Fri Apr 03 04:17:06 BST 2022 ------------
[  OK  ] Started Show Plymouth Boot Screen.
[  OK  ] Started Forward Password Requests to Plymouth Directory Watch.
[  OK  ] Reached target Local Encrypted Volumes.
[  OK  ] Found device /dev/disk/by-partuuid/238f45c7-01.
         Starting File System Check…isk/by-partuuid/238f45c7-01...
[  OK  ] Finished Load/Save Random Seed.
[  OK  ] Finished File System Check…/disk/by-partuuid/238f45c7-01.
         Mounting /boot...
[  OK  ] Mounted /boot.

Changing the Number of Lines head Retrieves

The head command allows you to control the number of lines it will retrieve from the specified file.

You can utilize the “-n NUM” or “--lines=NUM” options to control the number of lines. Using this, you can output as many or as few lines as you want.

head -n [-]NUM [FILE]...

If you preface the “NUM” value with a minus symbol like “-NUM“, the head command will print all lines from the top-down, except for the amount specified.

Example of Setting the Number of Lines for the head Command

For this example, we will use Linux’s head command to only print the top 5 lines from a file. We will be using our systems syslog file to showcase the output from this command.

We need to use “head“, followed by the “-n” option, our number of lines, 5, and finally, the syslog file path.

head -n 5 /var/log/syslog

From the output below, you can see that the head command retrieved only five lines from the top of our file.

Apr  3 00:00:11 pimylifeup systemd[1]: rsyslog.service: Sent signal SIGHUP to main process 504 (rsyslogd) on client request.
Apr  3 00:00:13 pimylifeup systemd[1]: logrotate.service: Succeeded.
Apr  3 00:00:13 pimylifeup systemd[1]: Finished Rotate log files.
Apr  3 00:00:14 pimylifeup systemd[1]: man-db.service: Succeeded.
Apr  3 00:00:14 pimylifeup systemd[1]: Finished Daily man-db regeneration.

Example of Getting All But Bottom Lines from the head Command

As mentioned earlier, using the minus symbol (-) when you specify the number of lines will grab all lines but the amount specified.

The tail command has similar behavior, utilizing the plus symbol (+) to grab all but the top lines.

For the following example, we will grab all lines from our boot log file, excluding the bottom 70 lines from the output.

head -n -70 /var/log/boot.log

Below is an example of how head grabbed every line from our file, except for the bottom 70 lines.

------------ Fri Apr 01 04:17:06 BST 2022 ------------
[  OK  ] Started Show Plymouth Boot Screen.
[  OK  ] Started Forward Password Requests to Plymouth Directory Watch.
[  OK  ] Reached target Local Encrypted Volumes.
[  OK  ] Found device /dev/disk/by-partuuid/238f45c7-01.
         Starting File System Check…isk/by-partuuid/238f45c7-01...
[  OK  ] Finished Load/Save Random Seed.
[  OK  ] Finished File System Check…/disk/by-partuuid/238f45c7-01.
         Mounting /boot...
[  OK  ] Mounted /boot.
[  OK  ] Reached target Local File Systems.
         Starting Set console font and keymap...
         Starting Raise network interfaces...
         Starting Preprocess NFS configuration...
         Starting Tell Plymouth To Write Out Runtime Data...
         Starting Create Volatile Files and Directories...
[  OK  ] Finished Set console font and keymap.
[  OK  ] Finished Preprocess NFS configuration.
[  OK  ] Reached target NFS client services.
[  OK  ] Reached target Remote File Systems (Pre).
[  OK  ] Reached target Remote File Systems.
[  OK  ] Finished Create Volatile Files and Directories.
         Starting Network Time Synchronization...
         Starting Update UTMP about System Boot/Shutdown...
[  OK  ] Finished Tell Plymouth To Write Out Runtime Data.
[  OK  ] Finished Update UTMP about System Boot/Shutdown.
[  OK  ] Finished Raise network interfaces.
[  OK  ] Started Network Time Synchronization.
[  OK  ] Reached target System Time Set.
[  OK  ] Reached target System Time Synchronized.
[  OK  ] Listening on Load/Save RF …itch Status /dev/rfkill Watch.
         Starting Load/Save RF Kill Switch Status...
[  OK  ] Started Load/Save RF Kill Switch Status.

Limiting head Output by Bytes

Linux’s head command also allows you to deal with a number of bytes rather than the number of lines.

By utilizing the “-c NUM” or “--bytes=NUM” options, we can control how many bytes worth of data the command will output.

This option is useful when using the head tool on a binary file or a text file where you only want a number of bytes retrieved.

head -c [-]NUM [FILES]...

Like the “lines” option, you can also use the minus symbol (-) to output all data except for the number of bytes at the end of the file.

Example of Limiting by Bytes with the head Command

For this example, we will use the head command and the “-c” option only to get the first 256 bytes from the specified file.

The file we will be using to showcase this is our Linux systems syslog located at “/var/log/syslog“.

head -c 256 /var/log/syslog

As shown below, you can see that the head tool grabbed only 256 bytes worth of characters from the top of the file.

Apr  3 00:00:11 pimylifeup systemd[1]: rsyslog.service: Sent signal SIGHUP to main process 504 (rsyslogd) on client request.
Apr  3 00:00:13 pimylifeup systemd[1]: logrotate.service: Succeeded.
Apr  3 00:00:13 pimylifeup systemd[1]: Finished Rotate log fil

Example of Getting All Data Except for Number of Bytes

The minus sign will also affect the “bytes” option. Using this, you can grab the entirety of a file but skip the last number of bytes in the file.

You need to use “-c“, followed by the minus symbol (-) and the number of bytes you want to be skipped. Finally, you need to specify the file for the head command to interact with.

head -c -512 /var/log/boot.log

Below is an example of how the output would look when using the bytes option. We have trimmed the output, but you can still see the bottom of the result where data was cut.

[  OK  ] Started WPA supplicant.
[  OK  ] Reached target Network.
         Starting Modem Manager...
         Starting CUPS Scheduler...
         Starting /etc/rc.local Compatibility...
         Starting OpenBSD Secure Shell server...
         Starting Permit User Sessions...
[  OK  ] Started triggerhappy global hotkey daemon.
[  OK  ] Started LSB: rng-tools (Debian variant).
[  OK  

Disabling the head Commands Headers on Linux

If you need Linux’s head command to not display headers in its output, then you can utilize the “-q“, “--quiet” or “--silent” option.

These options tell the head option that it should never print headers for files in its output.

Using the quiet option is especially useful when dealing with data where you don’t need them separated.

head -q [FILES]...

Example of using the Quiet option with head

To show you how the quiet option works, we will try using the head command on both our “syslog” and “boot.log” files.

To use the quiet option, you only need to use “head“, followed by the “-q” option, and finally, the files you are reading from.

head -q /var/log/syslog /var/log/boot.log

Below, you can see despite using two files, no headers were ever printed, and the head tool merged the results.

Apr  3 00:00:11 pimylifeup systemd[1]: rsyslog.service: Sent signal SIGHUP to main process 504 (rsyslogd) on client request.
Apr  3 00:00:13 pimylifeup systemd[1]: logrotate.service: Succeeded.
Apr  3 00:00:13 pimylifeup systemd[1]: Finished Rotate log files.
Apr  3 00:00:14 pimylifeup systemd[1]: man-db.service: Succeeded.
Apr  3 00:00:14 pimylifeup systemd[1]: Finished Daily man-db regeneration.
Apr  3 00:00:15 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:15 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so
Apr  3 00:00:15 pimylifeup brltty[1065]: screen driver not loadable: a2
Apr  3 00:00:20 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:20 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so

Displaying Headers on Linux’s head Output

By default, Linux’s head command will not display headers when only one file is passed into the tool.

We can utilize the “-v” or “--verbose” option to change this behavior. With this option used, the head tool will always print the headers for files.

head -v [FILES]...

Example of Displaying Headers with the head Command

For this example, we will be showing how the verbose option works when using the head tool on a single file.

We only need to use “head“, followed by the “-v” option, then our example log file located at “/var/log/syslog“.

head -v /var/log/syslog

The result below shows that the header has been printed for our single file.

==> /var/log/syslog <==
Apr  3 00:00:11 pimylifeup systemd[1]: rsyslog.service: Sent signal SIGHUP to main process 504 (rsyslogd) on client request.
Apr  3 00:00:13 pimylifeup systemd[1]: logrotate.service: Succeeded.
Apr  3 00:00:13 pimylifeup systemd[1]: Finished Rotate log files.
Apr  3 00:00:14 pimylifeup systemd[1]: man-db.service: Succeeded.
Apr  3 00:00:14 pimylifeup systemd[1]: Finished Daily man-db regeneration.
Apr  3 00:00:15 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:15 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so
Apr  3 00:00:15 pimylifeup brltty[1065]: screen driver not loadable: a2
Apr  3 00:00:20 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:20 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so

Combining the head and tail Commands on Linux

The head command is commonly combined with the tail command. This is because you can get the top part of a file, then use the tail command to get the end of that file.

You achieve this by using the pipe (|) to pass the results from head to tail. The most basic syntax for achieving this is what we have shown below.

head [OPTIONS] [FILE] | tail [OPTIONS]

Example of Combining the head and tail Commands

For this example, we will split this explanation into two parts.

The first part of this example uses the head command. It will grab the first 30 lines of the “/var/log/syslog“.

The result of the head tool is passed to the tail command using the pipe (|). With tail, we use the “-n” option to grab the last 5 lines of heads output.

head -n 30 /var/log/syslog | tail -n 5

Below, we ended up with only 5 lines from the overall output.

Apr  3 00:00:45 pimylifeup brltty[1065]: screen driver not loadable: a2
Apr  3 00:00:50 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory
Apr  3 00:00:50 pimylifeup brltty[1065]: cannot load screen driver: /lib/brltty/libbrlttyxa2.so
Apr  3 00:00:50 pimylifeup brltty[1065]: screen driver not loadable: a2
Apr  3 00:00:55 pimylifeup brltty[1065]: /lib/brltty/libbrlttyxa2.so: cannot open shared object file: No such file or directory

Conclusion

You should now understand the various ways to use the head command on your Linux system.

Head is helpful when you need to easily grab the top lines from a file. You can control this to grab several lines or a number of bytes.

If you have any questions about using this command, please comment below.

Be sure to check out our many other Linux command guides as well as our general Linux tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *