
head Command in Linux
Linux has various command-line utilities like cat, less, and view that you can use to display the content of a file. However, these command-line options are useful for viewing large contents of files. If you only need to display a specific number of lines in a file, the head command comes into play.
- You can use the head command to display the first few lines of a file or files. By default, it shows the first 10 lines. If you specify multiple files, the head will show the first 10 lines of each file, and it will label the output with the file names to distinguish between them.
- You can change the number of lines that head displays by using the -n option followed by the number of lines you want to see. For example, "-n" 5 will show the first 5 lines instead of the default 10.
- Instead of lines, you can limit the output to a specific number of bytes using the -c option followed by the number of bytes. For example, "-c" 20 will show the first 20 bytes of the file.
For compatibility with older scripts, head supports an older syntax where options are written in a different format. This format is -[NUM][bkm][clqv] where NUM is a number and b, k, and m are size letters that stand for blocks, kilobytes, and megabytes, respectively.
When you use NUM with the "-c" option, it specifies the number of bytes to display. For example, -c 100 means show the first 100 bytes of the file. When you use NUM with the "-l" option, it specifies the number of lines to display. For example, -l 10 means show the first 10 lines of the file.
Note − The "-l" option is not commonly used in modern scripts; instead, the "-n" option is preferred. For current Linux systems, it is recommended to use "-c" NUM for bytes and "-n" NUM for lines. This ensures compatibility and clarity in your scripts.
In addition, the head command returns an exit status to indicate whether it was successful or not. An exit status of 0 means the command was successful. A nonzero exit status means there was an error, such as the file not being found or being unreadable.
Table of Contents
Here is a comprehensive guide to the options available with the head command −
- Syntax for the head Command
- Options Available for the head Command
- Examples of head Command in Linux
Syntax for the head Command
The following is the general syntax for the head Command
head [OPTION]... [FILE]...
Options Available for the head Command
The following is a list of options for the head command that controls how the command processes and displays file content −
Tag | Description |
---|---|
-c, --bytes=[-]NUM | Prints the first NUM bytes of each file. If NUM is preceded by a -, it prints all but the last NUM bytes. |
-n, --lines=[-]NUM | Prints the first NUM lines instead of the default first 10 lines. If NUM is preceded by a -, it prints all but the last NUM lines. |
-q, --quiet, --silent | Suppresses the printing of file name headers. |
-v, --verbose | Always prints file name headers. |
-z, --zero-terminated | Uses a NUL character instead of a newline as the line delimiter. |
--help | Displays help information and exits. |
--version | Outputs version information and exits. |
Examples of head Command in Linux
In this section, we'll explore various examples of the head command using the options we've discussed above to help you understand how to use it effectively −
Display the First 10 lines of a File
To perform this, weâll first create a file named example.txt with the following content −

Now, to display the first 10 lines of this file, you can use the following command −
head example.txt

Display the First 5 Lines
To display the first 5 lines of a file, you can use the head command with the -n 5 option −
head -n 5 example.txt

Display the First 20 bytes
To display the first 20 bytes from the beginning of a file, you can use the head command with the "-c" option −
head -c 20 example.txt

Using the Obsolete Option Syntax
You can also use the obsolete option syntax, where you directly specify the number of lines without the "-n" flag −
head -4 example.txt
This command displays the first 4 lines of filename.txt.

Using Size Letters with Bytes
You can also use size letters to specify the number of bytes. To achieve this, you can run the following command −
head -c 10k example.txt
This command displays the first 10 kilobytes (10,240 bytes) of filename.txt.

Suppress File Name Headers
You can use the head command with the "-q" option to suppress the file name headers when displaying the contents of multiple files −
head -q filename1.txt filename2.txt
This command displays the first 10 lines of filename1.txt followed by the first 10 lines of filename2.txt without showing the file name headers. This is useful when you want to view the contents of multiple files as a continuous stream of text.

Show File Name Headers
You can use the "-v" flag with the head command to ensure that the file name headers are always displayed, even if only one file is specified −
head -v filename1.txt
This command displays the first 10 lines of filename.txt and includes the file name header. This is useful for clarity, especially when working with multiple files or when you want to ensure the source of the displayed content is always clear.

Using head With Other Commands
The head command is versatile and can be combined with other commands using pipes to perform various tasks. Here are some common combinations −
You can use head with grep to filter the first few lines of a file for a specific pattern −
head example.txt | grep "Li"
This command displays all lines in the first 10 lines of example.txt that contain the pattern âLi.â

You can combine head with wc (word count) to give you the count of lines, words, and bytes in the first few lines of a file −
head example.txt | wc
This command outputs the number of lines, words, and bytes in the first 10 lines of example.txt.

You can use head and tail together to display a specific range of lines from a file −
head -n 10 example.txt | tail -n 5
This command first shows the first 10 lines of example.txt and then pipes that output to tail, which displays the last 5 lines of those 10 lines.

You can combine head with sort to display the first few sorted lines of a file −
sort example.txt | head -n 5
This command sorts example.txt and then displays the first 5 lines of the sorted output.

You can use head with awk to help you process and format the first few lines of a file −
head -n 5 example.txt | awk '{print $1, $3}'
This command takes the first 5 lines of example.txt and uses awk to print the first and third columns of each line.

You can also use head with cut to display specific fields from the first few lines of a file −
head -n 5 filename2.txt | cut -d' ' -f 1,3
This command displays the first and third fields (assuming space as the delimiter) from the first 5 lines of example.txt.

Conclusion
The head command in Linux is a powerful and efficient tool for quickly displaying the initial lines or bytes of files, making it useful when working with large datasets. With options to customize output, such as specifying the number of lines or bytes to display, as well as suppressing or displaying file name headers, head offers flexibility that can enhance your workflow.
Understanding its syntax and available options, including the legacy formats, ensures compatibility with various scripts and systems. Moreover, when combined with other commands, head can be integrated into complex workflows to filter and manipulate data effectively.
Whether you're a seasoned developer or getting your feet wet to the command line, mastering the head command will undoubtedly improve your efficiency in file management and data processing tasks in Linux.