The head
command in Linux is a simple yet powerful utility used to display the first few lines of a text file or output. It’s commonly used when you want to quickly preview a file without opening the entire content, making it invaluable for administrators and developers.

This guide will walk you through how to use the head
command, its syntax, options, and practical use cases to enhance your Linux skills.
Prerequisites
- Linux-based system (Ubuntu, CentOS, etc.).
- Familiar with basic terminal commands and navigation.
- Access to text files or output from commands to practice with.
The head
Command Syntax
The basic syntax of the head
command in Linux is as follows:
head [options] [file(s)]
[options]
: Optional flags that modify the command’s behavior.[file(s)]
: The text file(s) from which to display the first few lines.
head Command Options
The head
command in Linux comes with several options that allow you to customize its output:
-n [number]
: Display the firstn
lines of a file.-c [bytes]
: Display the firstn
bytes instead of lines.-v
: Show the file name along with its content.-q
: Suppress the file name output (useful when processing multiple files).-n
(default): If no option is provided, thehead
command displays the first 10 lines by default.
How to Use the head
Command in Linux
The head
command in Linux is a useful tool for quickly viewing the beginning of a file or output. It displays the first few lines or bytes of a file, allowing users to preview content without opening the entire file.
Here are a few practical examples of how the head
command can be used:
Display First 10 Lines of a File:
To view the first 10 lines of a file, you can use the following command:
head -n 10 example.txt
This is the default usage of the head
command. It shows the first 10 lines of the file example.txt
.
Output:
Line 1: This is the first line of the file.
Line 2: This is the second line of the file.
Line 3: This is the third line of the file.
Line 4: This is the fourth line of the file.
Line 5: This is the fifth line of the file.
Line 6: This is the sixth line of the file.
Line 7: This is the seventh line of the file.
Line 8: This is the eighth line of the file.
Line 9: This is the ninth line of the file.
Line 10: This is the tenth line of the file.
Check Out | How to Install Vim Editor on Ubuntu and Master Basic Commands
Display First 20 Lines of a File:
To view the first 20 lines of a file, you can use the following command:
head -n 20 example.txt
This command shows the first 20 lines of example.txt
. The -n
option allows you to specify the exact number of lines you want to display.
Output:
Line 1: This is the first line of the file.
Line 2: This is the second line of the file.
...
Line 20: This is the twentieth line of the file.
Display First 50 Bytes of a File:
The -c
option allows you to display the first 50 bytes of the file. Unlike lines, it’s based on the file’s byte size.
head -c 50 example.txt
Output:
This is the first 50 bytes of the file: This is the first line of the file.
Preview First Few Lines of Output from Another Command
You can pipe the output of other commands into head
to see the first few lines. For example, dmesg
shows the kernel ring buffer messages, and head
limits it to the first 10 lines.
dmesg | head
Output:
[ 0.000000] Booting Linux 5.4.0-42-generic...
[ 0.000000] Linux version 5.4.0-42-generic...
...
Show First 5 Lines of Each File
This command shows the first 5 lines of both file1.txt
and file2.txt
. It’s useful when you want a quick preview of multiple files.
head -n 5 file1.txt file2.txt
Output:
==> file1.txt <==
Line 1: First line of file1.
Line 2: Second line of file1.
Line 3: Third line of file1.
Line 4: Fourth line of file1.
Line 5: Fifth line of file1.
==> file2.txt <==
Line 1: First line of file2.
Line 2: Second line of file2.
Line 3: Third line of file2.
Line 4: Fourth line of file2.
Line 5: Fifth line of file2.
head Command Use Cases
The head
command can be incredibly useful in a variety of scenarios. It allows you to quickly preview the beginning of a file or command output, saving you time and effort when working with large files or logs. Here are some common use cases:
Quickly Preview Log Files:
If you want to view the beginning of a log file (e.g., /var/log/syslog
), head
can show you the most recent entries or configurations.
head -n 20 /var/log/syslog
Output:
Jan 1 00:00:01 server kernel: [ 0.000000] Booting Linux...
Jan 1 00:00:01 server systemd[1]: Started Session 1 of user root.
Jan 1 00:01:01 server sshd[100]: Accepted password for root from 192.168.1.2 port 22 ssh2
Jan 1 00:05:12 server systemd[1]: Started Daily apt upgrade and clean activities.
...
Check the First Few Lines of a Large File:
When working with large text files, it’s often helpful to inspect the top lines to understand the content or format.
head -n 30 largefile.txt
Output:
Line 1: Welcome to the large file.
Line 2: This file contains multiple sections.
Line 3: Section 1: Introduction
Line 4: Section 2: Data Overview
Line 5: Section 3: Analysis
...
Line 30: Section 10: Conclusion
Pipe Command Output to Head:
You can also pipe the output of other commands into head
to limit the results. This is useful when running commands that produce long output.
ps aux | head -n 10
Output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.4 19000 5000 ? Ss 10:00 0:20 /sbin/init
user1 1200 0.1 0.3 16000 2300 pts/0 Ss 10:05 0:10 bash
user2 2005 0.0 0.2 12000 3000 pts/1 S+ 10:06 0:05 python3
...
Conclusion
The head
command in Linux is a simple yet incredibly useful tool for quickly viewing the top part of files or command outputs. Whether you need to preview logs, check the start of large files, or manage command outputs, head
can significantly boost your productivity in the Linux terminal.