Shell Scripting Kali Linux Ubuntu Red Hat CentOS Docker in Linux Kubernetes in Linux Linux interview question
in Linux Linux interview question Python R Java C
Next Article: Head Command in Linux With
Tail command in Examples
Linux with…
examples Last Updated : 10 Mar, 2025
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
1. Preview Files: Instantly view the start of log
files, configuration files, or datasets.
2. Customize Output: Adjust the number of lines
or bytes displayed.
3. Combine with Other Commands:
Pipe head with tools like grep, tail, or sort 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
type head followed by the filename. For
example, to display the first 10 lines of a file
named example.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 and file2.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 of
file.txt.
-c <number>: Specifies the number of
bytes to display. For example, head -c 100
file.txt will display the first 100 bytes of
file.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 invoke head with the
file name:
head example.txt
Comment More info Next Article
Tail command in Linux
Advertise with us
with examples
Similar Reads
Linux Commands
Linux commands are essential for controlling and managing the system
through the terminal. This terminal is similar to the command prompt in…
Windows. It’s important to note that Linux/Unix commands are case-
15+ min
sensitive. read commands are used for tasks like file handling, process
These
management, user adm
File and Directory Manipulation
File Operations and Compression
Network and Connectivity
How to Monitor System Activity in Linux | top Command
top command is used to show the Linux processes. It provides a dynamic
real-time view of the running system. Usually, this command shows the…
summary information of the system and the list of processes or threads
10 are
which mincurrently
read managed by the Linux Kernel. As soon as you will
run this command it
How to Kill a Process in Linux | Kill Command
kill command in Linux (located in /bin/kill), is a built-in command which is
used to terminate processes manually. kill command sends a signal to …
process that terminates the process. If the user doesn't specify any
6 min
signal thatread
is to be sent along with the kill command, then a default
TERM signal i
ifconfig Command
Knowing your IP address is fundamental for network administration,
troubleshooting, and various Linux system tasks. In this article, we will…
explore several methods to find your IP address in a Linux environment.
10 minyou
Whether read
are a seasoned Linux user or just getting started,
understanding these meth
How to Check Network Connectivity in Linux | ping Command
Ensuring a stable and reliable internet connection is crucial for seamless
navigation and efficient communication in the world of Linux. The "ping…
command is a powerful tool that allows users to check the status of their
7 minconnection
internet read and diagnose network-related issues. In this article,
we w
How to use SSH to connect to a remote server in Linux | ssh…
Command
Secure Shell, commonly known as SSH, is like a super-secure way to talk
to faraway computers, called servers. It's like a secret tunnel on the…
internet that keeps your conversations safe and private. Imagine you're
8 minaread
sending letter, and instead of sending it openly, you put it in a magic
envelope th
How to Securely Copy Files in Linux | scp Command
Secure file transfer is a crucial part of Linux systems administration.
Whether moving sensitive files between local machines or transferring…
data between servers, SCP (Secure Copy Protocol) provides a fast,
10 min
secure, andread
efficient way to copy files over a network. By utilizing SSH
(Secure Shell), SCP
Wget Command in Linux/Unix
Wget is the non-interactive network downloader which is used to
download files from the server even when the user has not logged on t…
the system and it can work in the background without hindering the
6 min
current read GNU wget is a free utility for non-interactive download
process.
of files from the Web. It
curl Command in Linux with Examples
In the realm of Linux, mastering the command line is essential for
efficient and powerful usage of the operating system. Among the…
number of command line tools available, `curl` stands out as one of the
most6 min read and powerful utilities. Originally developed by Daniel
versatile
Stenberg, `curl` is a comma
How to Compare Files Line by Line in Linux | di! Command
In the world of Linux, managing and comparing files is a common task
for system administrators and developers alike. The ability to compare…
files line by line is crucial for identifying differences, debugging code, and
9 min the
ensuring readintegrity of data. One powerful tool that facilitates this
process i
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,
6 min readfiles, CSV datasets, and other text documents in seconds.
configuration
The head comma
Text Processing and Manipulation
Help and Information
System Administration and Control
User and Group Management
Privilege and Security Management
Process Management and Control
Company Languages DSA Data Science Web Python
About Us Python Data Structures & ML Technologies Tutorial
Corporate & Legal Java Algorithms Data Science With HTML Python
Communications Address:
Privacy Policy C++ DSA for Beginners Python CSS Programming
A-143, 7th Floor, Sovereign
In Media PHP Basic DSA Data Science For JavaScript Examples
Corporate Tower, Sector-
Contact Us GoLang Problems
136, Noida, Uttar Pradesh Beginner TypeScript Python Projects
(201305) Advertise with us SQL DSA Roadmap Machine Learning ReactJS Python Tkinter
GFG Corporate R Language Top 100 DSA ML Maths NextJS Web Scraping
Registered Address: Solution Android Tutorial Interview Data Visualisation Bootstrap OpenCV Tutorial
K 061, Tower K, Gulshan Placement Tutorials Archive Problems Pandas Web Design Python Interview
Vivante Apartment, Sector Training Program DSA Roadmap by NumPy Question
137, Noida, Gautam Buddh
GeeksforGeeks Sandeep Jain
Nagar, Uttar Pradesh, NLP Django
201305 Community All Cheat Sheets Deep Learning
Computer DevOps System Inteview School GeeksforGeeks
Science Git Design Preparation Subjects Videos
Operating Linux High Level Design Competitive Mathematics DSA
Systems AWS Low Level Design Programming Physics Python
Computer Docker UML Diagrams Top DS or Algo for Chemistry Java
Network Kubernetes Interview Guide CP Biology C++
Database Azure Design Patterns Company-Wise Social Science Web
Advertise with us
Management GCP OOAD Recruitment English Grammar Development
System DevOps Roadmap System Design Process Commerce Data Science
So!ware Bootcamp Company-Wise World GK CS Subjects
Engineering Interview Preparation
Digital Logic Questions Aptitude
Design Preparation
Engineering Puzzles
Maths
So!ware
Development
So!ware Testing
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
Got It !
@GeeksforGeeks, Sanchhaya Education andPrivate
understood our Cookie
Limited, Policy
All rights & Privacy Policy
reserved