C.W.K.
Stream
Lesson 08 of 10 · published

wc, head, tail

~10 min · wc, head, tail, follow

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Three trivial-looking tools you'll use forever

wc counts. head shows the top. tail shows the bottom. Each of them has just one or two flags worth knowing, and you'll wear those flags out.

wc — word count

  • wc -l file — line count. The 90% case.
  • wc -w file — word count.
  • wc -c file — byte count.
  • wc -m file — character count (different from bytes for UTF-8).

From a pipe: find . | wc -l counts files. git log --oneline | wc -l counts commits. cat file | wc -c is the manual size of a file.

head — first N

  • head file — first 10 lines (default).
  • head -n 20 file — first 20.
  • head -n -5 file — all but the last 5 (GNU only).
  • head -c 100 file — first 100 bytes.

tail — last N

  • tail file — last 10 lines.
  • tail -n 50 file — last 50.
  • tail -n +10 file — from line 10 onward.
  • tail -f file — follow new lines as they're appended. Essential for live logs.
  • tail -F file — follow even if the file is rotated.

The killer combo

tail -f /var/log/system.log | grep -i error — live error stream. find . -name '*.py' | head — preview a search. history | tail -20 — recent commands.

Code

Live log tailing·bash
# macOS unified log alternative
log stream --predicate 'eventMessage CONTAINS "error"' --info
# Plain file
tail -F /var/log/system.log | grep -i pippa
Common counting recipes·bash
find . -name '*.ts' -type f | wc -l       # TS file count
git log --oneline | wc -l                 # commits
wc -l < access.log                        # log line count (no filename)
find . -name '*.py' -exec wc -l {} + | tail -1   # total LOC

External links

Exercise

On a Mac log: tail -f /var/log/system.log and watch lines stream. Ctrl+C. Then count files in your project: find . -type f | wc -l. Then head -3 ~/.zshrc and tail -10 ~/.zshrc.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.