Sep 14, 2023 4 min read

Linux Head Command

Use the Linux head command with our step-by-step tutorial. The head command prints the first 10 lines of files or piped data by default.

Linux Head Command
Table of Contents


The head command prints the first 10 lines of one or more files or piped data to standard output by default.

It is often used to quickly inspect the beginning of a file or to extract a preview of its content. The "head" command is quite versatile and allows you to customize the number of lines to display, specify multiple files for processing, and combine it with other commands using pipelines for more complex operations.

In this tutorial, you will understand head command in Linux. We will also address a few FAQs on head command in Linux.

Head Command Syntax

Below is the syntax of the head command:

head [OPTION]... [FILE]...
  • OPTION - head options. In the next sections, we'll go over the most frequent options.
  • FILE - Names of zero or more input files. Head will read the standard input if no FILE is specified or if FILE is -.

How to Use the head Command

When used without any options, the head command displays the first ten lines in its most basic form.

head filename.txt

Display a Specific Number of Lines

Use the -n (--lines) option, then an integer, to determine the number of lines to display:

head -n <NUMBER> filename.txt

You can just use the hyphen (-) and the number instead of the letter n. (with no space between them).

If you want to see the first 30 lines of a file named filename.txt, type:

head -n 30 filename.txt

The following command will produce the same results as the ones listed above:

head -30 filename.txt

Display a Specific Number of Bytes

The -c (--bytes) option specifies the number of bytes to print:

head -c <NUMBER> filename.txt

To display the first 100 bytes of data from a file named filename.txt, for example, type:

head -c 100 filename.txt

You can also define the number of bytes to be displayed by appending a multiplier suffix to the number. b multiplies it by 512, kb multiplies it by 1000, k multiplies it by 1024, MB multiplies it by 1000000, M multiplies it by 1048576, and so on.

The following command will display the first five kilobytes (2048) of the file filename.txt:

head -c 5k filename.txt

Display Multiple Files

If the head command is given multiple files as input, it will display the first ten lines from each file.

head filename1.txt filename2.txt

The same options as when displaying a single file are available.

The first 20 lines of the files filename1.txt and filename2.txt are shown in this example:

head -n 20 filename1.txt filename2.txt

When more than one file is used, the content of each file is preceded by a header that displays the file name.

Use head with Other Commands

By using pipes to redirect standard output from/to other utilities, the head command can be used in conjunction with other commands.

The following command hashes the $RANDOM environment variable displays the first 32 bytes, and generates a random string of 24 characters:

echo $RANDOM | sha512sum | head -c 24 ; echo

FAQs on head command in Linux

Can I display a specific number of bytes instead of lines using "head"? 

Yes, the "-c" option allows you to display a specific number of bytes instead of lines. For instance, head -c 100 file.txt will show the first 100 bytes of "file.txt".

How can I make "head" display a different number of lines instead of the default 10?

You can specify the desired number of lines using the "-n" option. For example, head -n 5 file.txt will display the first 5 lines of "file.txt".

Can I use "head" with multiple files at once? 

Yes, you can provide multiple filenames as arguments to the "head" command, and it will display the specified number of lines from each file. For example, head -n 3 file1.txt file2.txt will display the first 3 lines from "file1.txt" and "file2.txt".

How can I use "head" in combination with other commands using a pipeline? 

You can use the output of another command as input to "head" using the pipe symbol (|). For example, ls -l | head -n 5 will display the first 5 lines of the output from the "ls -l" command.

Is there a way to view the entire file except for the first N lines using "head"? 

Yes, you can use the "-n" option along with a plus sign (+) and the offset value to exclude the first N lines. For instance, head -n +6 file.txt will display all lines in "file.txt" except for the first 5.

Can "head" display the first N lines continuously as data is appended to a file? 

Yes, you can use the "-f" or "--follow" option with "head" to continuously display the first N lines of a file as new data is appended. For example, tail -f -n 20 file.txt will show the last 20 lines of "file.txt" and update in real-time.

Can "head" display the first part of a file in a reverse order?

No, "head" is designed to display lines from the beginning of a file. To display lines in reverse order, you can utilize the "tail" command with appropriate options.


You should now be able to use the Linux head command with confidence. It's a companion to the tail command, which publishes the file's last lines to the terminal.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.