The head command displays the beginning of a file or input stream. It is useful for checking headers, previewing structured data, or sampling output without printing everything.
Text-Fu · Lesson 8
head
Learn how to display a controlled number of lines or bytes from the beginning of input.
Displaying the First Ten Lines
With no count option, head prints the first 10 lines of each named file:
$ head events.log
The file is not modified. If it contains fewer than 10 lines, all available lines are printed.
What does head events.log print by default?
Choosing a Line Count
Use -n NUMBER to choose how many lines to print:
$ head -n 15 events.log
GNU head also accepts the compact form -15, but -n 15 states the option's meaning more clearly.
Which command displays the first five lines of report.txt?
Choosing a Byte Count
Use -c NUMBER when you need bytes rather than complete lines:
$ head -c 20 archive.bin
This prints the first 20 bytes. The output may end in the middle of a text line or, for multibyte text, in the middle of an encoded character. Use line mode for ordinary text previews.
Which command writes the first 100 bytes of payload.bin to stdout?
Reading stdin and Multiple Files
When no file operand is supplied, head reads stdin:
$ generate-report | head -n 5
When several files are named, head normally adds a header identifying each file's output:
$ head -n 2 january.txt february.txt
==> january.txt <==
...
==> february.txt <==
...
Use -q to suppress these headers or -v to show a header even for one file.
In generate-report | head -n 5, what does head read?
Which option suppresses filename headers when head reads several files?
To practice previewing file beginnings, try these hands-on labs:
- Linux head Command: File Beginning Display - This lab will guide you through using the
headcommand to display the initial lines of text files, including modifying the line count. - Viewing Log and Configuration Files in Linux - Practice essential Linux command-line skills for efficiently viewing and navigating text files, including system logs and configuration files, which often require commands like
head. - Rapid Threat Detection - Apply your knowledge of
head(andtail) to quickly extract and analyze recent log entries, simulating real-world cybersecurity analysis.
Lesson complete
You finished head
You can now preview the beginning of files and command output with head.
Use the default first-ten-line view.
Select a line count with
-n.Select a byte count with
-cwhen appropriate.Read from stdin in a pipeline.
Control headers when several files are shown.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account