head, tail & wc
Three small tools for sizing up a file: head shows the beginning, tail the end, and wc (word count) counts lines, words and bytes. tail -f is also the classic way to watch a log file grow in real time.
head
head prints the first 10 lines by default; -n N chooses how many:
seq 1 15 > lines.txt
head lines.txt | tr '\n' ' '; echo
head -n 3 lines.txt | tr '\n' ' '; echo
head -n -12 lines.txt | tr '\n' ' '; echo1 2 3 4 5 6 7 8 9 10
1 2 3
1 2 3
head -n -12 (GNU) means "everything except the last 12 lines". head -c 20 prints the first 20 bytes instead of lines.
A typical use is peeking at a data file's header and first rows:
cat > orders.csv <<'CSV'
order_id,customer,total
1001,ada,59.90
1002,linus,12.50
1003,grace,240.00
1004,ada,18.75
CSV
head -n 1 orders.csv
echo "---"
head -n 3 orders.csv | column -t -s,order_id,customer,total
---
order_id customer total
1001 ada 59.90
1002 linus 12.50
column -t aligns the columns into a readable table (-s, tells it the separator).
tail
tail prints the last 10 lines; -n N chooses how many, and -n +N starts at line N:
seq 1 15 > lines.txt
tail -n 3 lines.txt | tr '\n' ' '; echo
tail -n +13 lines.txt | tr '\n' ' '; echo
echo "--- skip a CSV header:"
printf 'name,qty\npens,3\ncups,5\n' > stock.csv
tail -n +2 stock.csv13 14 15
13 14 15
--- skip a CSV header:
pens,3
cups,5
tail -n +2 is the standard way to drop a header line before processing data.
head and tail together
To get lines from the middle, chain them: lines 6 to 8 are "the first 8, then the last 3 of those":
seq 1 15 > lines.txt
head -n 8 lines.txt | tail -n 3 | tr '\n' ' '; echo
sed -n '6,8p' lines.txt | tr '\n' ' '; echo6 7 8
6 7 8
Following a growing file: tail -f
tail -f keeps the file open and prints new lines as they're appended: the go-to command when watching a service's log while you test it. Press Ctrl+C to stop.
tail -f /var/log/nginx/access.log
tail -F app.log # also survives log rotation (file replaced)
tail -f app.log | grep --line-buffered ERRORIn a script you can bound it with timeout. Here a background loop writes to a log while tail -f follows it for a moment:
: > live.log
( for i in 1 2 3; do echo "event $i"; sleep 0.3; done >> live.log ) &
timeout 2 tail -n +1 -f live.log
echo "tail stopped with status $? (124 = stopped by timeout)"event 1
event 2
event 3
tail stopped with status 124 (124 = stopped by timeout)
wc: counting
| Option | Counts |
|---|---|
-l |
Lines (newline characters) |
-w |
Words (separated by whitespace) |
-c |
Bytes |
-m |
Characters (differs from bytes for UTF-8 text) |
-L |
Length of the longest line |
printf 'the quick brown fox\njumps over\nthe lazy dog\n' > pangram.txt
wc pangram.txt
wc -l pangram.txt
wc -w < pangram.txt
printf 'café\n' | wc -c
printf 'café\n' | wc -m 3 9 44 pangram.txt
3 pangram.txt
9
6
5
With a file name, wc prints the name too; reading from a redirect (< file) or pipe gives just the number, which is what you want in a script: count=$(wc -l < file). é takes two bytes in UTF-8, so the byte and character counts differ.
Counting in pipelines
mkdir -p src
printf 'a\nb\nc\n' > src/one.py
printf 'x\ny\n' > src/two.py
printf 'TODO fix\nok\nTODO test\n' > src/notes.md
echo "files: $(find src -type f | wc -l)"
echo "python lines: $(cat src/*.py | wc -l)"
echo "TODOs: $(grep -r TODO src | wc -l)"
wc -l src/*.pyfiles: 3
python lines: 5
TODOs: 2
3 src/one.py
2 src/two.py
5 total
With several files wc prints a line per file and a total line.
Note:
wc -lcounts newline characters, so a last line without a trailing newline isn't counted:printf 'a\nb' | wc -lprints1.
Test yourself
Which command prints everything from line 2 to the end?
- head -n 2 file
- tail -n 2 file
- tail -n +2 file
- head -n +2 file
How do you get only the number of lines, without the file name?
- wc -l file
- wc -l < file
- wc file
- wc -n file
What is tail -f used for?
- Printing the first lines
- Following a file and printing new lines as they're written
- Formatting columns
- Fixing line endings