How to Use the Linux head Command

By
Marko Aleksic
Published:
December 16, 2025
Topics:

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.

Prerequisites

  • A system running Linux.
  • Command-line access.
  • Administrative privileges.

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 desired 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--quietDo not separate the content of multiple files with a file name tag.

How to Use head Command in Linux

Follow the steps below to learn how to use the Linux head command on an example.

1. Create and open the file:

sudo nano example1.txt

2. 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.

3. 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.

4. 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 to standard output, they are written to the desired 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 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