head — First Lines of a File
Ctrl+F:
head -n 200· first lines · preview ·-cbytes
head outputs the beginning of a file (default: 10 lines). Pair with tail for the end, grep to filter first, less for interactive scroll.
Full text-processing hub → Text Processing
Syntax
head [OPTIONS] [FILE...]
command | head [OPTIONS]📌 Flags
| Flag | Long | Description |
|---|---|---|
-n NUM | --lines=NUM | Print first NUM lines (default 10) |
-NUM | — | Shorthand: head -200 file = first 200 lines |
-c NUM | --bytes=NUM | Print first NUM bytes (not lines) |
-q | --quiet | No ==> file <== headers when multiple files |
-v | --verbose | Always print filename headers |
-z | --zero-terminated | Line delimiter is NUL, not newline (GNU) |
head --help📌 Examples — line count
head file.txt # First 10 lines (default)
head -n 20 file.txt # First 20 lines
head -n 200 file.txt # First 200 lines
head -200 file.txt # Same — shorthand (-200 = -n 200)
# Multiple files
head -n 5 /etc/passwd /etc/group
head -qv -n 3 file1 file2 # Headers + 3 lines eachSave first 200 lines to a new file
head -n 200 huge.log > first200.log
head -200 nmap_full.txt > nmap_top200.txtPipes
curl -s http://TARGET/ | head -n 50
grep -r "password" /var/www 2>/dev/null | head -n 20
cat access.log | head -n 100
nmap -sC -sV TARGET | head -n 30📌 Examples — bytes (-c)
head -c 100 file.bin # First 100 bytes
head -c 1K file.txt # First 1 KB (GNU: K, M, G suffixes)
head -c 512 /etc/passwd📌 OSCP use cases
# Preview big scan without scrolling
head -n 50 nmap/allports.txt
# First lines of a looted config
head -n 30 /var/www/html/config.php
# Quick peek at log before grep
head -n 200 /var/log/apache2/access.log
# First users in passwd
head -n 15 /etc/passwd
# Limit noisy command output
find / -name "*.db" 2>/dev/null | head -n 20📌 head vs tail (quick)
| Goal | Command |
|---|---|
| First 200 lines | head -n 200 file |
| Last 200 lines | tail -n 200 file |
| Lines 201–400 (skip first 200) | tail -n +201 file | head -n 200 |
| Follow live log | tail -f /var/log/syslog |
📌 Quick cheat sheet
head file.txt
head -n 200 file.txt
head -200 file.txt
head -n 200 big.txt > top200.txt
command | head -n 50
head -c 256 binary.dat | xxd