← All 155 books wc, one count per page
One count per page

wc, one count per page

Twenty-eight lessons on the wc command, one count per page: what wc is and the three counts of the plain run; the count flags -l for lines, -w for words, -c for bytes and -m for characters, joined flags like -lwc and the fixed order of the columns; the total line when files are listed on the command line, the dash for standard input and counting streams through pipes; counting matches from grep with grep -c and grep piped into wc -l, and counting JSON with jq length; the newline that makes a line and the file with no trailing newline; bytes against characters under UTF-8, the locale surprise, the hidden byte order mark, counting a binary file, the du disk cousin and the longest line with -L; and teaching a friend one honest hour.


Steve Hodgkiss 7 lessons

One count, one page, one honest hour. That's the rhythm.

wc, one count per page

Twenty-eight lessons on the wc command, one count per page: what wc is and the three counts of the plain run; the count flags -l for lines, -w for words, -c for bytes and -m for characters, joined flags like -lwc and the fixed order of the columns; the total line when files are listed on the command line, the dash for standard input and counting streams through pipes; counting matches from grep with grep -c and grep piped into wc -l, and counting JSON with jq length; the newline that makes a line and the file with no trailing newline; bytes against characters under UTF-8, the locale surprise, the hidden byte order mark, counting a binary file, the du disk cousin and the longest line with -L; and teaching a friend one honest hour.


Set in Space Grotesk, Inter and JetBrains Mono (SIL Open Font License).

Every fact in this book is as the official source states it, fetched and read during this build: the local manual pages wc(1), du(1), grep(1), cat(1), cut(1) and uniq(1) (GNU coreutils 9.4), the definition of truth for this book, and the Wikipedia articles Wc (Unix), Newline, Byte, Character encoding, Byte order mark, Grep, du (Unix), uniq, Cut (Unix), jq (programming language) and Locale (computer software). No facts are sourced from Reddit, Stack Overflow, vendor blogs or commercial sites. Every command and output pair was run on this machine with temporary files under the build scratch directory only, each capture cached verbatim so the pair is reproducible. SAFETY LAW: no file outside the book directory's scratch area was written by any demonstration; every run was read-only, counting files that already existed. Honest notes: the Wikipedia titles jq, Uniq (Unix) and Comm (Unix) are redirect stubs, so each was followed and read at its redirect goal (jq (programming language), uniq and Comm); and Locales redirected to Locale, a disambiguation page, and was followed on to Locale (computer software). An independent guide, not affiliated with or endorsed by the Free Software Foundation, the IEEE, or the Wikimedia Foundation.

Your purchase is for personal use only. You do not have redistribution rights: please do not share, resell, or republish this book or its pages.

© 2026 Steve Hodgkiss. All rights reserved. Personal use only; no redistribution rights.

Edition 1.0 · stevehodgkiss.net

Contents

Contents


Part 1 · The plain count
Words with -w4
Part 2 · The count flags
Joined flags5
Part 3 · Files and stdin
The dash is stdin6
Part 4 · The pipe
Counting JSON7
Part 5 · The newline law
Bytes are not chars8
Part 6 · Surprises
The hidden BOM9
Part 7 · The long view
Teaching wc10

Per the wc(1) manual page, -w, --words prints the word counts, and a word is a non-zero-length sequence of printable characters delimited by white space. Per the Wikipedia Wc (Unix) article usage list, wc -w prints the word count. This machine's capture: wc notes.txt printed 3 13 60 notes.txt, with 13 the word count.

Counting tools companion · No. 01
the rule

Words with -w

the word count and its rule

what counts as a word$ wc notes.txtcountwordsbytheblankswhite space does the cutting13 words in notes.txtwc -w13a word: non-zero-length,printable characters, blank-delimited, per the manualthe flag counts what the blanks leave behind

What is a word? The wc(1) manual page defines it: a non-zero-length sequence of printable characters delimited by white space. No dictionary, just blanks doing the cutting.

Per the manual page, -w exists to print the word counts; the Wikipedia usage list says wc -w prints the word count.

Our capture: notes.txt holds 13 words under that rule. Count a line by hand, then trust the flag once your rule matches its rule.

ONE HONEST HOUR

Write a sentence you can count by eye, run wc -w on it, and check your hand count against the flag.

Per the wc(1) manual page, wc [OPTION] takes any of the count options and prints the selected counts always in the order newline, word, character, byte, maximum line length. This machine's captures: wc -l -w -c notes.txt printed 3 13 60 notes.txt, and wc -lwc notes.txt printed 3 13 60 notes.txt, identical.

Counting tools companion · No. 02
the cluster

Joined flags

wc -lwc means -l -w -c

three flags, one dash, same answer$ wc -l -w -c notes.txt3 13 60 notes.txt$ wc -lwc notes.txt3 13 60 notes.txtidentical, bytefor byte-l-w-c-lwcseparate flags, one command:the joined form is the samerequest, read l, then w, then cwc -lwc is wc -l -w -c: identical answers, byte for byte

The flags cluster. wc -lwc is exactly wc -l -w -c: one dash, three letters, the same request.

Our captures prove it: wc -l -w -c notes.txt printed 3 13 60 notes.txt, and wc -lwc notes.txt printed 3 13 60 notes.txt. Byte for byte the same answer.

The joined form is the one you will see in other people's scripts, so read it as a list, never as a word: l then w then c, three counts requested.

ONE HONEST HOUR

Run wc -l -w -c and wc -lwc on the same file, and confirm with your eyes that the two answers match.

Per the wc(1) manual page, with no FILE, or when FILE is -, wc reads standard input. This machine's capture: wc - read the same notes stream and printed 3 13 60 with a dash standing in the file name column.

Counting tools companion · No. 03
the dash

The dash is stdin

the dash names the stream

the dash names the stream$ wc -3 13 60 -a dash where the file name sitsthe count happened; the namebelongs to a stream, not a filemix it with real files:wc notes.txt -two FILEs for wc: the file andthe dash, so the total rowfollows, per the manual pageFILE is a dash: standard input, wearing a file name

The dash is the explicit form of listening. Per the wc(1) manual page, "with no FILE, or when FILE is -, read standard input": the dash names the stream.

Our capture: wc - read our notes stream and printed 3 13 60 with a dash where the file name would sit, so you can see it counted a stream, not a file.

Mix it freely among real files: wc notes.txt - counts the file, then the pipe, and totals them. The dash is a file that is a pipe.

ONE HONEST HOUR

Feed the same text to wc with no argument and with the dash, and spot where the dash appears in the answer.

Per the Wikipedia jq (programming language) article, jq is a widely-used command-line utility designed for processing JSON data, and programs consist of filters that can be composed in pipelines. This machine's capture: jq length on a three-element JSON array printed 3, and on a two-key object printed 2.

Counting tools companion · No. 04
the JSON count

Counting JSON

jq length counts JSON

counting inside JSON[{"n":1},{"n":2},{"n":3}]123jq length3{"apples":3,"pears":5}jq length again2two keys countedwhen the stream is JSON, jq length asks the counting question

JSON is not lines, so wc -l is the wrong question. Per the Wikipedia jq article, jq is "a widely-used" command-line utility designed for processing JSON data, and programs consist of filters "that can be composed in" pipelines.

The counting filter is length. Our capture: a three-element array piped through jq length answered 3; a two-key object answered 2.

Elements and keys, not bytes: when the stream is JSON, let jq ask the question and let wc rest.

ONE HONEST HOUR

Pipe a small JSON array through jq length, then a small object, and say what each count is counting.

Per the Wikipedia Character encoding article, in UTF-8 code points map to a sequence of one, two, three or four code units. Per the Wikipedia Byte article, the byte commonly consists of eight bits. This machine's capture: wc -m -c utf8.txt printed 33 39 utf8.txt.

Counting tools companion · No. 05
the split

Bytes are not chars

39 bytes, 33 characters

one character, several byte cellséone character8 bits8 bitsa byte eachone character, several byte cells$ wc -m -c utf8.txt33 39 utf8.txtcharsbytesin UTF-8, code points map toone, two, three or four unitsnever call a byte count a character count: choose on purpose

The gap between 33 and 39 has a cause. Per the Wikipedia Character encoding article, in UTF-8 code points map to a sequence of one, two, three or four code units, and each byte commonly consists of eight bits.

Our capture: wc -m -c utf8.txt printed 33 39 utf8.txt. Thirty-three characters; some of them weighed more than one byte, and the byte column added the difference.

The red rule: never call a byte count a character count. Choose -m or -c on purpose, and say which you chose.

ONE HONEST HOUR

Add two accented names to a plain file, watch wc -m and wc -c pull apart, and explain the gap in one sentence.

Per the Wikipedia Byte order mark article, the appearance of the BOM as a magic number at the start of a text stream can signal several things to a program reading the text, the UTF-8 representation of the BOM is the byte sequence EF BB BF, and BOM use is optional. This machine's related capture: wc -m -c utf8.txt separated characters from bytes at 33 and 39.

Counting tools companion · No. 06
the ghost

The hidden BOM

three bytes you cannot see

three bytes at the startutf8.txtEFBBBFthe byte order mark: optional,invisible, still counted by -cyour editor hides ityour byte count sees it33 chars, 39 bytesthe gap is where it hidesthe mark is real storage: measure, do not assume

Some files begin with three bytes you will never see. Per the Wikipedia Byte order mark article, a BOM at the start of a text stream can signal several things to a program reading the text, and the UTF-8 form is the byte sequence EF BB BF.

Its presence is real storage: BOM use is optional, the article says, and wc -c counts it. Our utf8 capture split characters from bytes at 33 and 39; a mark lives in that gap.

Red edge: byte counts see it, your editor may hide it. Measure, do not assume.

ONE HONEST HOUR

Save a small file with and without a byte order mark, run wc -c on both, and state the difference.

Per this book's plan, the last lesson hands wc to a friend in one honest hour: five captured lines from this book, typed by the learner, read aloud by the teacher. This machine's captures behind the five lines: 3 13 60 notes.txt, the same from wc -lwc, 0 3 19 nonl.txt, 33 39 utf8.txt, and 1024 blob.bin.

Counting tools companion · No. 07
pass it on

Teaching wc

one honest hour, one friend

five lines, one hour, one friend13 13 60 notes.txt2wc -lwc, the same answer30 3 19 nonl.txt433 39 utf8.txt51024 blob.binwhen they can explain whyzero lines was still real text,the hour is donethey type, you read aloud,one honest hour, per the planpass the counter on: the last count of the book is a friend

The last count is not a flag: it is passing the tool on, one friend, one honest hour, per this book's plan.

They type; you read aloud. The five lines are this book's own captures: 3 13 60 notes.txt, the same from wc -lwc, the trap 0 3 19 nonl.txt, the split 33 39, and 1024 blob.bin.

When they can say why zero lines was still real text, the hour is done and the counter is theirs.

ONE HONEST HOUR

Sit with one friend and teach wc in one sitting; they type the five lines, you read each count aloud.

Index

Index


Bytes are not chars8
Counting JSON7
Joined flags5
Teaching wc10
The dash is stdin6
The hidden BOM9
Words with -w4