head — view the beginning of a file
💬 "head command"
head displays the first N lines of a file. It is useful for checking the header of a formatted file.
Use it to quickly understand the structure before opening a large file.
- Concept: Prints the beginning of a file (10 lines by default).
- Left argument: Use -n number to specify the number of lines.
- Right argument: The target file.
- Multiple files: With multiple files, it shows the beginning of each file along with a per-file header.
Commonly used options
| Option | Description |
|---|---|
| -n <N> | Prints the first N lines |
| -q | Suppresses file name headers when processing multiple files |
| -c <N> | Prints the first N bytes instead of lines |
3 essential command combinations
head -n 1 /var/log/nginx/access.log— Check the format with the first log linehead -n 3 data.csv— Check the first 3 CSV header lineshead -c 64 /opt/app/bin/app.bin | xxd | head— Check the binary signature
💬 "Give me an example from a security audit perspective"
Here are common situations on a production server, organized in order.
Step 1 · Check the format
Understand the structure with the first few lines of the file.
head -n 2 /etc/nginx/nginx.conf
Step 2 · Compare samples
Compare the beginning of several files.
head -n 2 /var/log/nginx/*.log
Step 3 · Check binary
View byte-level output if necessary.
head -c 32 /opt/app/bin/app | xxd
How it is used in practice
1. Check CSV columns
Situation — You need to check the column order of an uploaded CSV first.
head -n 3 /srv/import/orders.csv
Result — Check the column order and delimiter with the header and two sample lines.
Point — Checking the format with head before writing an import script reduces trial and error.
2. Check the first block of a config file
Situation — You want to quickly see only the first structure among several server blocks.
head -n 20 /etc/nginx/nginx.conf
Result — Quickly understand the global settings and include structure.
Point — Use head to grasp the structure before opening the full file, then inspect in detail with less.
Points to watch
- The default is 10 lines, so unless you specify the number of lines, you may not get enough information.
- If you set the number of lines too high, using head loses its purpose.
- If a CSV has fields containing line breaks, simple line counting will be off.
Actual output
This is the result of checking the header and sample lines of a CSV file.
!head command output: CSV header and samples
Production server
Source (HTML): https://games.aidebatehub.com/programs/linux/logs/head.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments