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.
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
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.
|show the specified number of lines|
|show the specified number of bytes|
|show the file name tag|
|don'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
With the sample file in place, you can run the
head command by typing:
The output lists the first 10 lines in the file, as in the image below.
Displaying Specific Number of Lines
head displays the first 10 lines.
To change the number of lines in the output, add the
--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
Displaying Specific Number of Bytes
Another option is to define the number of bytes in the output. To do so, use the
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
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
head -v file_name
For instance, to display the name tag along with the output of our sample file, run:
head -v example1.txt
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.
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
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
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
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.
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.