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:

Bash

Now basic commands:

Bash

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

Bash

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:

Bash

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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *