Head Command in Linux With Examples
Need to quickly view the beginning of a file in Linux? The head command is your best option. This essential command-line tool enables users, developers, and system administrators to preview the start of log files, configuration files, CSV datasets, and other text documents in seconds.
The head command is useful for troubleshooting issues, monitoring logs, and managing large datasets, as it allows you to view specific portions of data without opening entire files. In this guide, we will explore the head command in Linux, covering its syntax, options, and practical examples for better understanding.

Head Command in Linux With Examples
What is the head Command in Linux
The head command in Linux is a command-line utility used to display the first few lines (or bytes) of one or more text files. By default, it outputs the first 10 lines of a file, making it invaluable for quickly previewing content without opening large files entirely. It’s widely used in system administration, log analysis, data processing, and scripting to extract or inspect critical information efficiently.
Key Features of the head
Command
- Preview Files: Instantly view the start of log files, configuration files, or datasets.
- Customize Output: Adjust the number of lines or bytes displayed.
- Combine with Other Commands: Pipe
head
with tools likegrep
,tail
, orsort
for advanced workflows.
Syntax:
head [options] [file(s)]
where,
- Options: are the flags you use to customize the behaviour of head
- File(s): is the name of the file/s you’re looking to view.
Let us consider two files having name state.txt and capital.txt contains all the names of the Indian states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal
$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Srinagar
Without any option, it displays only the first 10 lines of the file specified.
Example:
$ head state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Options
1. -n num: Prints the first ‘num’ lines instead of first 10 lines. num is mandatory to be specified in command otherwise it displays an error.
$ head -n 5 state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
2. -c num: Prints the first ‘num’ bytes from the file specified. Newline count as a single character, so if head prints out a newline, it will count it as a byte. num is mandatory to be specified in command otherwise displays an error.
$ head -c 6 state.txt
Andhra
3. -q: It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.
Without using -q option $ head state.txt capital.txt ==> state.txt <== Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir ==> capital.txt <== Hyderabad Itanagar Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar With using -q option $ head -q state.txt capital.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Hyderabad Itanagar Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar
4. -v: By using this option, data from the specified file is always preceded by its file name.
$ head -v state.txt
==> state.txt <==
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Real-World Scenario of head Command
Now, we’re going to discuss some of the best real-world scenarios that can be used using head command in Linux:
1. Print line between M and N lines(M>N)
For this purpose, we use the head, tail, and pipeline(|) commands. The command is: head -M file_name | tail +N since the head command takes first M lines and from M lines tail command cuts lines starting from +N till the end, we can also use head -M file_name | tail +(M-N+1) command since the head command takes first M lines and from M lines tail command cuts (M-N+1) lines starting from the end. Let say from the state.txt file we have to print lines between 10 and 20.
$ head -n 20 state.txt | tail -10
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
2. How to use the head with pipeline(|)
The head command can be piped with other commands. In the following example, the output of the ls command is piped to head to show only the three most recently modified files or folders.
Display all recently modified or recently used files. $ ls -t e.txt d.txt c.txt b.txt a.txt Cut three most recently used file. $ ls -t | head -n 3 e.txt d.txt c.txt
3. Multiple Pipping for Additional Processing
It can also be piped with one or more filters for additional processing. For example, the sort filter could be used to sort the three most recently used files or folders in the alphabetic order.
$ ls -t | head -n 3 | sort
c.txt
d.txt
e.txt
4. For Viewing Huge Log Files
There are number of other filters or commands along which we use head command. Mainly, it can be used for viewing huge log files in Unix.
?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
Conclusion
The head command in Linux is an essential tool for quickly viewing the btop lines of files. Whether you need to display a specific number of lines, view files in a custom format, or pipe output from other commands, head command for Linux makes it easy to manipulate the text files.
Head command in Linux with examples – FAQs
What does the head command do in Linux?
The
head
command in Linux is used to display the first few lines of a file or files to the standard output. By default, it shows the first 10 lines.
How to use the head command with a file?
To use the
head
command with a file, simply typehead
followed by the filename. For example, to display the first 10 lines of a file namedexample.txt
, you would use:head example.txt
Can I use the head command to display byte insteas of lines?
Yes, you can use -c option to display bytes, here’s the syntax for it:
head -c 10 filename
How do I display the first 20 lines of a file using head command?
You can use the -n option, syntax for this:
head -n 20 filename
Can we use the head command with multiple files?
Yes, you can use the
head
command with multiple files. When used with more than one file,head
will display the first 10 lines from each file with a header showing the file name. For example:head file1.txt file2.txt
This command will display the first 10 lines from both
file1.txt
andfile2.txt
.
What are some common options for the head command?
Some common options for the
head
command include:
-n <number>
: Specifies the number of lines to display. For example,head -n 5 file.txt
will display the first 5 lines offile.txt
.-c <number>
: Specifies the number of bytes to display. For example,head -c 100 file.txt
will display the first 100 bytes offile.txt
.
How to display the first 10 lines of a file using head?
To display the first 10 lines of a file using the
head
command, simply invokehead
with the file name:head example.txt