How to Use the Linux head Command

January 5, 2022

Introduction

The Linux head command prints the first lines of one or more files (or piped data) to standard output. By default, it shows the first 10 lines. However, head provides several arguments you can use to modify the output.

Read on to learn how to use the head command, its syntax, and options with easy-to-follow examples.

How to use the Linux head command.

head Command in Linux Syntax

The syntax for using the head command is:

head [option] file_name

You can run the command with or without additional options (arguments).

head Command Options

The head command options allow you to modify the output and display the wanted amount of data. Each option has its short and long form, which you can add to the basic syntax.

They include:

OptionLong-FormDescription
-n--linesshow the specified number of lines
-c--bytesshow the specified number of bytes
-v--verboseshow the file name tag
-q--quietdon't separate the content of multiple files with a file name tag

How to Use head Command in Linux

To demonstrate how to use the Linux head command, let's create a sample file named example1.txt that contains 13 lines of text listing 13 U.S. states.

First, create and open the file:

sudo nano example1.txt

Now, add the following content:

Connecticut
Delaware
Georgia
Maryland
Massachusetts
New Hampshire
New Jersey
New York
North Carolina
Pennsylvania
Rhode Island
South Carolina
Virginia

Save and exit the file (press Ctrl+X and then Y to confirm). You can check the contents of the file using the cat command:

cat example1.txt
Create a sample file to practice working with head command with an example.

With the sample file in place, you can run the head command by typing:

head example1.txt

The output lists the first 10 lines in the file, as in the image below.

How to use the Linux head command with example.

Displaying Specific Number of Lines

By default, head displays the first 10 lines.

To change the number of lines in the output, add the -n (--lines) argument before the file name:

head -n [number] file_name

For instance, to show the first 4 lines of example1.txt, run:

head -n 4 example1.txt
Output 4 lines using the head command.

Displaying Specific Number of Bytes

Another option is to define the number of bytes in the output. To do so, use the -c (--bytes) argument:

head -c [number] file_name

Therefore, to see 20 bytes of output of the sample file, you would run the command:

head -c 20 example1.txt
Display 20 bytes of a file using the head command.

Note: To find a list of all important Linux commands, check out our Linux Commands Cheat Sheet and save it for future reference.

Displaying the File Name Tag

To display the file name before outputting the first 10 lines, add the -v (--verbose) option:

head -v file_name

For instance, to display the name tag along with the output of our sample file, run:

head -v example1.txt
Display the file name tag with the head command output.

Displaying Multiple Files

You can also display the first lines of multiple files using a single command:

head [option] file_name1 file_name2

To see the first lines of files example1.txt and example2.txt, you would type:

head example1.txt example2.txt

The output displays the name of each file before listing the first 10 lines of output.

Display multiple files with their name tag using the head command.

Additionally, you can modify the output of multiple files by adding other arguments. For example, to see the first four lines of each file, type:

head -n 4 example1.txt example2.txt
Displaying multiple files and specifying the number of lines using the head command.

Note: To compare multiple files and search for differences between them, use the Linux diff Command.

Redirecting Output to a Text File

You can redirect the output from the head command to a text file (or log file) using the greater-than sign (>). Instead of displaying the lines in standard output, they are placed into the wanted file.

If the specified file already exists, it will be overwritten. Otherwise, the command creates a new file under the specified name.

The syntax for redirecting output from the head command is:

head [options] file_name > output_file

For instance, to redirect the first 10 lines of example1.txt to a file named output1.txt, you would run the command:

head example1.txt > output1.txt

To check whether you successfully redirected the output, use the cat command:

cat output1.txt
Redirect output using the head command to a specific file.

Using head with Pipeline

head can be piped to one or more commands to modify the output:

[command] | head [option]

For example, to list files in the /etc directory using the ls command and print 10 entries, you would run the command:

ls /etc | head
Using the head command with pipeline.

Note: The ls command lists information about directories and any type of files in the working directory. To learn how to use crucial ls commands, check out 19 Crucial Linux ls Commands to Know.

Conclusion

The head command allows you to see the initial lines of a file in standard output without opening a file. In this article you learned how to use this utility and its options.

To display the entire contents of one or more files, it is better to use the Linux cat command.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Use the Linux cd Command to Change Directory
December 22, 2023

The cd command in Linux allows you to change the current working directory using the terminal...
Read more
How to Use the top Command in Linux
December 2, 2021

The top command is a built-in Linux utility that contains useful information about your system's...
Read more
How to Use Linux nohup Command
December 16, 2021

Learn how to use the nohup command to continue running commands and processes in Linux...
Read more
Using the Linux free Command
December 29, 2021

The free command in Linux is a utility that helps monitor RAM usage on a system...
Read more