Chapter 25: Bash View Start (head)
What does head actually do? (super simple first)
head = shows the first part (the “head”) of a file or input.
By default:
- Shows the first 10 lines of a file
- Very fast – doesn’t read the whole file (especially useful for huge files)
It’s the opposite of tail (which shows the end).
Most common real uses:
- Quickly see what a file looks like without opening it
- Check the header row of CSV files
- See the first few lines of a log / script / config
- Peek at the beginning of very large files (10 GB+ logs)
- Combine with other commands in pipelines
1. Basic usage (try these now!)
Let’s create a test file with many lines:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
cat > access.log << 'EOF' 2026-02-25 09:15:22 GET /home 200 1.2.3.4 Hyderabad 2026-02-25 09:15:45 GET /api/users 200 5.6.7.8 Delhi 2026-02-25 09:16:01 POST /login 401 9.10.11.12 Mumbai 2026-02-25 09:16:30 GET /dashboard 200 13.14.15.16 Bangalore 2026-02-25 09:17:10 GET /profile 304 17.18.19.20 Chennai 2026-02-25 09:17:55 GET /api/v2/products 200 21.22.23.24 Hyderabad 2026-02-25 09:18:20 HEAD /images/logo.png 200 25.26.27.28 Pune 2026-02-25 09:19:05 GET /cart 200 29.30.31.32 Kolkata 2026-02-25 09:19:40 POST /checkout 201 33.34.35.36 Ahmedabad 2026-02-25 09:20:15 GET /search?q=phone 200 37.38.39.40 Hyderabad 2026-02-25 09:21:00 GET /logout 200 41.42.43.44 Surat ... (imagine 1000 more lines here) EOF |
Now basic commands:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Show first 10 lines (default) head access.log # Show only first 5 lines head -n 5 access.log # or short way head -5 access.log # Show first 3 lines head -n 3 access.log |
2. Very common options (you’ll use these 90% of time)
| Option | What it does | Example command | Real use case |
|---|---|---|---|
| -n N or -N | Show first N lines | head -n 20 config.yaml | See structure |
| -c N | Show first N characters/bytes | head -c 200 short.txt | Small previews |
| -q | Quiet – no filename header (multiple files) | head -q -n 5 *.csv | Clean output |
| -v | Verbose – always show filename header | head -v -n 3 *.log | When checking many |
| +N (GNU head) | Show from line N to end | head -n +50 bigfile.txt | Skip first 49 lines (rare) |
3. Real-world examples you will use every day
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# See first few lines of a huge log file (instant!) head -n 15 /var/log/syslog # Check CSV header + first 5 rows head -n 6 sales-2026.csv # See first 100 characters of a script head -c 100 myscript.sh # Peek at multiple files quickly head -n 3 report*.txt # Combine with grep (see first few matching lines) grep "Hyderabad" access.log | head -n 8 # Get first field of first line (e.g. first date) head -1 access.log | awk '{print $1}' # First 10 error lines grep -i error app.log | head -n 10 # Check crontab or config file beginning crontab -l | head -n 5 cat /etc/nginx/nginx.conf | head -n 20 |
4. head vs tail – quick comparison
| Feature | head | tail |
|---|---|---|
| Shows | Beginning (first lines) | End (last lines) |
| Default lines | 10 | 10 |
| Live watching | No | Yes (tail -f) |
| Most used for | Headers, structure, preview | Latest logs, errors, status |
| Fast on huge files | Yes (reads only start) | Yes (jumps to end) |
5. Quick cheat-sheet table
| Goal | Command example | Notes |
|---|---|---|
| First 10 lines (default) | head filename.log | Everyday check |
| First N lines | head -n 20 error.log or head -20 error.log | Most common |
| First N characters | head -c 300 readme.md | Quick preview |
| Multiple files (quiet) | head -q -n 5 *.csv | No headers |
| From line N to end (skip top) | head -n +50 bigfile.txt | Rare but useful |
| Only first line | head -1 status.txt | Get header / first record |
| First line + cut field | head -1 data.csv |
cut -d ',' -f 1-3 |
| First few errors only | grep -i fail log.txt |
head -n 5 |
6. Pro tips from daily use
- head is instant even on 50 GB files – it never reads the whole thing
- Use head -n 20 when someone sends you a huge paste/log in chat
- head -1 + tail -1 = very common to see first & last line
- In scripts: head -n 1 to read config version / magic number
- Combine with less if you want to scroll after peeking: head -n 100 big.log | less
- No live follow mode (use tail -f for that)
Now open your terminal and try these 3 right now:
|
0 1 2 3 4 5 6 7 8 |
head -n 5 access.log head -n 3 employees.txt # if you still have it from earlier head -c 150 /etc/passwd # first 150 characters |
Tell me what you see! Or ask:
- “How to see first 10 lines of all .log files in a folder?”
- “head vs cat vs less – when to use which for big files?”
- “How to combine head and tail in one command?”
We’ll build exact commands together! 😄
