head (Output the first part of files)
The head command prints the first part (the head) of a file or piped data to the standard output. It is the perfect tool for checking headers, version numbers, or the initial structure of a document.
1. Basic Usage
head [options] filename
- By default,
headprints the first 10 lines of each file.
2. Key Options
| Option | Name | Description |
|---|---|---|
-n [number] |
lines | Prints the specified number of lines. (e.g., -n 15) |
-c [number] |
bytes | Prints the specified number of bytes. |
-q |
quiet | Never prints headers with file names. |
-v |
verbose | Always prints headers with file names. |
3. Practical Examples
① Viewing the first 5 lines
head -n 5 access.log
② Reading the first 100 bytes of a binary file
head -c 100 data.bin
③ Excluding the last N lines
To see everything EXCEPT the last 15 lines of a file:
head -n -15 list.txt
4. [Tip] Combining head and tail
To get a specific line, for example, only Line 7:
head -n 7 file.txt | tail -n 1
This takes the first 7 lines and then grabs the last line of that result.