Text-Fu · Lesson 8 of 16
head
0 / 5 checkpoints answered
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.
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.
CheckWhat does head events.log print by default?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command displays the first five lines of report.txt?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command writes the first 100 bytes of payload.bin to stdout?
press 1/2/3 to answer · → next lesson
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.
CheckIn generate-report | head -n 5, what does head read?
press 1/2/3 to answer · → next lesson
CheckWhich option suppresses filename headers when head reads several files?
press 1/2/3 to answer · → next lesson
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.
Summary
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.
Lesson complete
You finished head. Nice work — keep the momentum going.