Bash head
Command
Using the head
Command
The head
command is used to display the first part of files.
It's particularly useful for previewing the start of a file to understand its structure.
All examples below use the logfile.txt
file:
line 01
line 02
line 03
line 04
line 05
line 06
line 07
line 08
line 09
line 10
...
Basic Usage
The head
command displays the first 10 lines of a file by default:
Example: Display First 10 Lines
head logfile.txt
line 01
line 02
line 03
line 04
line 05
line 06
line 07
line 08
line 09
line 10
Options
The head
command has several options used to customize its behavior:
-n [number]
: Display the first [number] lines of the file.-c [number]
: Display the first [number] bytes of the file.
Option: -n [number]
The -n
option allows you to specify the number of lines to display from the start of the file. By default, head
shows the first 10 lines.
Example: Display First 5 Lines
head -n 5 logfile.txt
line 01
line 02
line 03
line 04
line 05
Option: -c [number]
The -c
option allows you to display the first [number] bytes of a file instead of lines.
Example: Display First 20 Bytes
head -c 20 logfile.txt
line 01
line 02
line
Option: Multiple Files
The head
command can be used to display the beginning of multiple files. By default, it prints the file name as a header before the content of each file.
Example: Display First 3 Lines of Multiple Files
ead -n 3 logfile.txt logfile2.txt
==> logfile.txt <==
line 01
line 02
line 03
==> logfile2.txt <==
line 01
line 02
line 03
Option: -q
The -q
option suppresses the printing of headers when multiple files are being processed. This is useful when you want to view the contents of multiple files without the file names being printed.
Example: Suppress Headers
head -q -n 3 logfile.txt logfile2.txt
line 01
line 02
line 03
line 01
line 02
line 03
Common Uses
The head
command is commonly used to:
- Preview the start of a file to understand its structure.
- Quickly check the contents of a file without opening it fully.
- Extract the header information from a data file.