Linux head command

The Linux head command is one of the most important tools on the command line. Its main purpose is to output the beginning of a (text) file or to limit the output of a Linux command.

Like the tail command, the Linux head command is part of the “GNU core utilities” (coreutils). The coreutils are a collection of basic command line commands that use a universal input and output format. They’re published with an open-source license and are available for a number of different operating systems.

Motivation behind the Linux head command

The Linux head command is used to limit the output of text on the command line. But why is that useful? To answer this question, let’s take a look at the general functionality of the Linux command line. Most Linux commands take text as input and/or give text as output. The target or source of a command could be:

  • Files, especially text files in ASCII format. Binary files like JPEG images or Word documents are less relevant here.
  • Standard input and output - Text that the user enters into the terminal or that the user is shown.
  • So-called “pipes”, which are chains of several Linux commands. The text output of one command is used as the text input of the next command.

And now consider that files themselves can contain vast amounts of text. For example, a file that’s several gigabytes large will contain billions of characters. From this perspective, it’s often useful to limit the amount of text output or only output specific lines or sections of text. Here’s an overview of common Linux commands for the output of text files:

  • Linux CAT: For outputting entire files. When it comes to large files, the command line is flooded with text.
  • Linux LESS: Outputs an entire file page by page. Not practical for large files.
  • Linux HEAD: Outputs the beginning of a file or limits the output of a file to a specific section.
  • Linux TAIL: Outputs the end of a file or limits the output to a specific section.

If this all sounds rather abstract, not to worry. In what follows we’ll look at some practical examples.

Executing Linux head on the command line

Linux head is a command line command. The name of the command can also be followed by optional parameters. The last part of the syntax contains the name of a file, several files, or a file path. Let’s first take a look at the general case:

head [options] <files>

The simplest form of the command, without options, looks as follows:

head <file>

When executed in this form, the Linux head command outputs the first 10 lines of the file. This way, you can quickly get a glimpse into the beginning of a file. This is often enough to get a sense of what the file is about.

The Linux head command is often used with the “-n” option, which allows you to specify how many lines should be output. Simply enter the number after “-n”. In the following example, only the first line of a file will be shown:

head -n 1 <file>

We could just as easily get the first 100 lines of a file as output:

head -n 100 <file>

Linux head command options

Like other Linux commands, Linux head can be fine-tuned with optional parameters. Since it’s part of the GNU coreutils, there’s a long form for every option; for the most frequently used options, there’s usually also a short form. Here’s an overview of the most useful options:

Option (Short form / long form) Explanation
-n / --lines Limits output to the first n lines / Excludes the last n lines from output
-c / --bytes Limits output to the first n bytes / Excludes the last n bytes from output
-q / --quiet, --silent Excludes file names when used with multiple files
-v / --verbose Includes file names when used with multiple files
--help Outputs help information for the command
Note

In the following examples we use the short form of the options (e.g. “-n” instead of “--lines”). This is the more common notation in documentation and code examples.

Excursus: Lines and characters

Before we look at some practical examples, let’s take a closer look at some terminology. What do we really mean when we’re talking about “lines” and “characters”?

Internally, a text file consists of a single, uninterrupted character string. The end of each line is marked only by a “line break” character. The character used for line breaks can vary from operating system to operating system, which sometimes leads to problems. In Linux, the “Line Feed” character (“LF”) indicates a line break.

When it comes to the “-c”/“--bytes” option, it’s assumed that a byte corresponds to exactly one character. This works for the ASCII character set but can lead to problems in other situations. The Unicode character set is a multibyte character encoding, meaning that several bytes might be used to represent a single character. If you’re processing a Unicode text file with the Linux head command, you might notice strange side effects. For example, certain special characters may not be displayed correctly.

Practical examples of the Linux head command

In the following examples, we’ll use the Universal Declaration of Human Rights (UDHR) as a concrete text example. With translations into over 460 languages, the UDHR is one of the most-translated texts in existence. We’ll start by downloading the text with the Linux curl command and saving it as “udhr.txt” on the desktop. Execute the following line of code in the command line:

curl https://www.unicode.org/udhr/d/udhr_eng.txt&gt; ~/Desktop/udhr.txt

Outputting only the first line using the Linux head command

We’ve already introduced the syntax for outputting the first line of a text file. Here’s what that looks like in the case of our concrete example:

head -n 1 ~/Desktop/udhr.txt
Tip

The output of the first line is especially interesting for viewing the so-called “shebang” or “hashbang” of a script file, which contains information about the interpreter used for execution of the script and its path.

Outputting everything except the last line using the Linux head command

If you want to output everything except the last line of a text, simply use the “-n” option and add a minus sign to the front of the number:

head -n -1 ~/Desktop/udhr.txt

Combining Linux head with the tail command to output specific sections of a text

And what if we want to output the preamble of the UDHR, from line 9 to 18? This is easily achieved using the sister command of Linux head, Linux tail. The Linux tail command is used to output only the end of a text file. If we combine the two commands with the pipe symbol “|”, we can zero in on a pre-defined section of the text:

head -n 18 ~/Desktop/udhr.txt | tail -n 10

We can also use this same formulation to output a single line from anywhere in the text. The tail command with the option “-n 1” will come in handy here. So, for example, to output line 28 “Everyone has the right to life, liberty and the security of person”, we’ll use the following command:

head -n 28 ~/Desktop/udhr.txt | tail -n 1

As you can see, the entire line is given as output. If we only want the part up to “security”, we can insert a further “piped” command. We’ll make use of the “-c” option and try different values as arguments until we get the exact section we’re looking for. In this case, we only need the first 62 characters:

head -n 28 ~/Desktop/udhr.txt | tail -n 1 | head -c 62

It might be more intuitive to count the number of characters that we want to cut out, starting at the end. This is also possible, using the “-c” option with a minus sign before the argument. The output of this command will be the same as in the previous example.

head -n 28 ~/Desktop/udhr.txt | tail -n 1 | head -c -12

Finally, let’s say we want to delete the spaces at the beginning of the document. Once again, we’ll use the tail command to do this. This time we’ll add a plus sign to the argument of the “-c” option. Just like the minus sign used with the head command, the plus sign used with the tail command reverses the meaning of the option: Instead of limiting the output to the last six characters of the text, we limit the output to everything that comes after the first six characters:

head -n 28 ~/Desktop/udhr.txt | tail -n 1 | head -c -12 | tail -c +6

You might be asking yourself if the effort involved in constructing such a complex command is worth it, or whether the same results can be achieved more easily. And indeed, this is just an illustrative example. The same operation can be completed more elegantly with the Sed command, though the Linux head command is faster when it comes to large files.

Filtering the output of the Linux head command with the grep command

You’ve now seen how the Linux head command can be combined with the tail command. Another useful command to combine with head is the grep command, which filters the output text. This way, we can limit the output to lines containing a certain search term. In the following example, we output the lines from the first 30 lines of the UDHR that contain the word “person”:

head -n 30 ~/Desktop/udhr.txt | grep person

Processing multiple files with the Linux head command

Up until now, we’ve been processing just a single file with the Linux head command. However, several files can be entered into the command at once. Simply list the file names/paths directly in the command or indicate an entire directory with files or a search pattern. We’ll show you the latter version here using the configuration files in the system directory “/etc/”. The files all have the ending “.conf”, which we use as a search pattern “*.conf”:

head -n 1 /etc/*.conf

In order to exclude file names from the output when processing multiple files, we can use the option “-q”:

head -q -n 1 /etc/*.conf

Or if we want to be sure to include the file names, we can use the option “-v”. This is useful in the case that only a single file turns up in the search:

head -v /etc/host*.conf

Listing the five most recently changed files with the Linux head command

The ls command is used for listing files and directories in Linux. With the “-t” option, we can sort the list in descending order, based on the date of the last change made to the file. And in order to cut off the list after five entries, we can add the head command as a pipe:

ls -t | head -n 5

Outputting a random hash with a certain length using the Linux head command

The Linux command “sha512sum” creates a hash with a length of 128 characters. In order to compare two hashes with your own eyes, it’s often not necessary to look at the entire hash. For example, sometimes it suffices to look at the first eight characters. You may be familiar with this pattern from Git Commits. To do this, we use the Linux head command with the “-c” option. We’ll take the bash function “$RANDOM” as a source, which will return a random number:

echo $RANDOM | sha512sum | head -c 8
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.