Back to Explore
GuideVibeFix editorialUpdated Dec 1, 2026

wc From Zero: Count Lines, Words and Bytes — One Command to See Through a File

How many lines, how many words, how many bytes — wc answers all three, and it is easy to get all three wrong. This guide runs every command for real on GNU coreutils wc 9.4: -l counts newlines (not lines), -c and -m diverge on Chinese text, --total=only, multi-file totals, and piping — with output pasted verbatim and the two classic traps explained.

Real photograph of an open book: close-up of printed pages (illustrative photo)

How many lines, how many words, how many bytes — these are the three questions you ask most often when you get hold of a file, and also the three easiest to guess wrong. wc answers all three. Its name is short for "word count," and its manual page defines it in one line: "print newline, word, and byte counts for each file." It belongs to GNU coreutils, the same toolbox as head, tail, cut, and grep, and it does exactly one thing: it counts. Every command in this guide was run for real on GNU coreutils wc 9.4, and the output is pasted verbatim.

Check the version first, then start. The demo files are generated on the spot by the script, with fixed contents, so the numbers are the same on every run:

$ wc --version
wc (GNU coreutils) 9.4
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin and David MacKenzie.
exit=0

There are five demo files, all written by the script with printf. three.txt is three lines of English, a few words per line. nonl.txt is a single line with no trailing newline. cjk.txt is two lines of Chinese. a.txt and b.txt are for the multi-file total demo. Look at the contents of three.txt first, then watch wc count it:

$ wc /tmp/wc-demo283/three.txt
 3  9 45 /tmp/wc-demo283/three.txt
exit=0

A bare wc with no options prints four things on one line: 3 is the line count, 9 is the word count, 45 is the byte count, and last comes the file name. The contents of three.txt are three lines of English — "alpha one", "beta two three", "gamma four five six" — and counting by hand gives exactly the same numbers: 3 lines, 9 words, 45 bytes, including the 3 newline characters. The order of the three numbers is fixed. The manual page says so: no matter which options you add, the output is always in the order "newline, word, character, byte, maximum line length." By default only the first three columns are shown — newline, word, byte. Character count and maximum line length need options before they appear.

To see just one column, add one option:

$ wc -l /tmp/wc-demo283/three.txt
3 /tmp/wc-demo283/three.txt
exit=0
$ wc -w /tmp/wc-demo283/three.txt
9 /tmp/wc-demo283/three.txt
exit=0
$ wc -c /tmp/wc-demo283/three.txt
45 /tmp/wc-demo283/three.txt
exit=0

-l is lines, -w is words, -c is bytes. The three options can be combined, and wc -l -w -c prints exactly the same output as a bare wc:

$ wc -l -w -c /tmp/wc-demo283/three.txt
 3  9 45 /tmp/wc-demo283/three.txt
exit=0

One formatting detail is worth noticing: the numbers are right-aligned in columns when several counts are printed. With a single option, there is no extra padding in front of the number. The formatting does not matter much, but do not let it fool you — when you parse wc output in a script, the safest approach is to take the first column with awk, or to ask wc for only the column you want in the first place.

Now the first trap. The contents of nonl.txt are "no trailing newline": 19 characters, with no newline at the end of the line:

$ wc -l /tmp/wc-demo283/nonl.txt
0 /tmp/wc-demo283/nonl.txt
exit=0
$ wc -c /tmp/wc-demo283/nonl.txt
19 /tmp/wc-demo283/nonl.txt
exit=0

The file clearly contains one line of text, yet wc -l says zero lines. This is not a bug. It is the definition of wc -l: it counts newline characters, not "lines." The manual page says "print newline counts," not "print line counts." A file with no trailing newline has zero newline characters, so wc -l is zero. This trap shows up in real life all the time: the last line of a log, a half-written file from a program, a text file copied over from Windows — any of them can be missing the final newline. Before you count lines with wc -l, ask yourself whether you want "the number of newline characters" or "the last line counts too." If you want the second, use grep -c '', or add a newline to the file first and then count.

The second trap is in the Chinese file. The contents of cjk.txt are two lines of Chinese: "ni hao shi jie" (four characters meaning "hello world") and "ni hao" (two characters meaning "hello"):

$ wc -c /tmp/wc-demo283/cjk.txt
20 /tmp/wc-demo283/cjk.txt
exit=0
$ LC_ALL=C.UTF-8 wc -m /tmp/wc-demo283/cjk.txt
8 /tmp/wc-demo283/cjk.txt
exit=0

The same file is 20 by one count and 8 by the other. Both numbers are correct; they count different things. -c counts bytes, -m counts characters. "Ni hao shi jie" is four Chinese characters, and in UTF-8 each Chinese character takes three bytes. "Ni hao" is two characters, six bytes. Add the two newline characters and you get 20 bytes and 8 characters. -m is --chars, the character count. -c is --bytes, the byte count. When you count Chinese, Japanese, or emoji — anything multi-byte — the difference between the two options is a factor of three. Pick the wrong one and your statistics are off by several times.

There is a subtler trap hiding behind -m: its result depends on the locale. In the demo script, the global locale is LC_ALL=C, and under the C locale wc -m is identical to wc -c — because the C locale does not recognize multi-byte characters, and one character is one byte. That is why the -m line in the demo is written as LC_ALL=C.UTF-8 wc -m, switching just that one command to a UTF-8 locale. The lesson: before counting Chinese characters, check that your locale is UTF-8. Run locale and look at whether LANG or LC_ALL carries UTF-8. If your terminal is on the C locale, wc -m will always equal wc -c, and your Chinese character counts will always be wrong.

The third count is -L, the maximum line length:

$ wc -L /tmp/wc-demo283/three.txt
19 /tmp/wc-demo283/three.txt
exit=0

The longest line in three.txt is "gamma four five six," 19 characters, so -L is 19. The manual page calls it "print the maximum display width" — note "display width," not byte count. For Chinese text, display width and character count can differ again, because a full-width character occupies two display columns. So -L on a Chinese file needs extra care. The everyday use of -L is to check whether a file has an abnormally long line: a log line that suddenly gets very long often means one field has run out of control; a configuration line that is far too long often means a newline went missing.

Count several files at once, and wc adds a total line at the end:

$ wc /tmp/wc-demo283/a.txt /tmp/wc-demo283/b.txt
 2  3 14 /tmp/wc-demo283/a.txt
 1  4 20 /tmp/wc-demo283/b.txt
 3  7 34 total
exit=0

a.txt is 2 lines, 3 words, 14 bytes; b.txt is 1 line, 4 words, 20 bytes; the total line is 3 lines, 7 words, 34 bytes. The total line only appears when more than one file is given. With a single file there is none. To see only the total, without the per-file detail, use --total=only:

$ wc --total=only /tmp/wc-demo283/a.txt /tmp/wc-demo283/b.txt
3 7 34
exit=0

--total takes two more values: auto is the default behavior (show the total for several files, hide it for one), never never shows a total, and always always shows one — even for a single file, where it adds an extra total line. In scripts, --total=only is the usual way to get a clean total number. If you want the full table — one line per file plus the total at the end — use the default auto.

When wc is given no file name, or a file name that is a single dash -, it reads standard input. The demo script feeds it a file with a redirect:

$ wc -l < /tmp/wc-demo283/three.txt
3
exit=0

The output is just the number 3, with no file name. Standard input has no name, so there is nothing for wc to print. This is most useful in pipelines: the output of any command can be piped straight into wc to be counted. The most common one-liner in the demo is counting the files in a directory:

$ ls /tmp/wc-demo283 | wc -l
5
exit=0

ls prints five file names, one per line, and wc -l counts five. Note what is actually being counted: the lines of ls output, not the files in the directory. If a file name contained a newline character — extremely rare, but possible — this trick would count wrong. Good enough for daily use, but know its limit. Another classic pipeline use is counting words:

$ printf 'a  b\tc\n' | wc -w
3
exit=0

The input is "a, two spaces, b, one tab, c," and wc -w says three words. The manual page defines a word as "a nonempty sequence of non white space delimited by white space characters or by start or end of input." Several consecutive whitespace characters — spaces or tabs — count as a single separator. So the two spaces between "a" and "b" still make only two words. That definition is accurate for English. For Chinese it needs care: Chinese words are not separated by spaces, so wc -w treats a whole Chinese sentence as a single word. Counting Chinese words needs a segmentation tool, not wc.

Empty input gives three zeros:

$ printf '' | wc -l -w -c
      0       0       0
exit=0

The output looks unremarkable, but it is useful in scripts: the simplest way to test whether a command produced any output is to pipe it into wc -l — zero means nothing came out. Notice that the three numbers are padded wide for empty input, because wc reserves space for the widest case when it does not know how big the input is. When parsing in a script, do not slice by fixed column positions. Take the column with awk instead.

When the file does not exist, wc prints an error, exits with code 1, and prints no count:

$ wc /tmp/wc-demo283/does-not-exist.txt
wc: /tmp/wc-demo283/does-not-exist.txt: No such file or directory
exit=1

If several files are given and one of them does not exist, wc counts the ones that do exist, skips the missing one with an error, and still exits 1 at the end. Be careful when using wc's exit code in a script to test whether "the files are all there." Exit code 1 only means "at least one file had a problem." It does not say which one. To find out which, read the standard error output.

How wc counts: each line of input passes through a newline counter, a word counter, and a byte and character counter, producing lines, words, and bytes; with several files a total line is added at the end

Finally, place wc in the toolbox alongside the guides already published in this series. head and tail show the beginning and end of a file; they answer "what does this file look like." grep finds the lines that match; it answers "which lines do I want." cut slices the fields out of each line; it answers "which column of each line." wc counts lines, words, and bytes; it answers "how many." The four are often chained together: grep filters the lines first, then wc -l counts how many survived; cut slices out a field first, then sort | uniq -c counts how often each value appears. Note that uniq -c counts "how many times each distinct value appears after de-duplication," while wc -l counts "how many lines there are in total." The two numbers answer different questions. df counts disk space, wc counts text. Both have "count" in the name, but one counts a filesystem and the other counts the contents of a file. Do not confuse them.

Here is a daily cheat sheet you can copy as-is. Count the error lines in a log: grep -c 'ERROR' app.log, or grep 'ERROR' app.log | wc -l. The first is grep's own counting; the second pipes to wc. Both give the same result, so pick whichever feels natural. Count the files in a directory: ls | wc -l. Count the lines in a code file: wc -l main.py, remembering that it counts newlines, so a missing trailing newline undercounts by one. Count the words in an English article: wc -w article.txt. Count the characters in a Chinese article: wc -m article.txt, after confirming the locale is UTF-8. Check whether a file has an abnormally long line: wc -L data.csv, then look at what the longest line actually contains. Get the total across several files: wc --total=only *.txt, which gives only the total and no per-file detail. Count standard input: cat file | wc -l, or the cheaper wc -l < file, which starts one fewer process.

A few more traps worth collecting. First: wc -l counts newlines, not lines. A file missing its trailing newline undercounts by one, as demonstrated above. Second: wc -m depends on the locale. Under the C locale it equals wc -c, and Chinese character counts come out wrong, also demonstrated above. Third: wc -w does not work for Chinese. A whole Chinese sentence with no spaces counts as a single word. Fourth: ls | wc -l counts the lines of ls output. A file name containing a newline breaks it — a rare case, but one to know about. Fifth: wc's numbers are right-aligned, and the column width changes with the size of the numbers. Parse with awk by column, not by fixed character position. Sixth: wc does not look at what a file is. It only counts. Feed it an image file and it will dutifully count the bytes. The number is correct, and meaningless.

The manual page has two more options this guide does not demonstrate in full; it is enough to know they exist. --files0-from=F reads a list of file names separated by NUL characters (\0) from file F and counts them. When file names contain spaces, newlines, or other special characters, ordinary argument passing breaks; NUL separation does not. --debug shows which line-count acceleration method wc used internally. wc optimizes counting newlines, and this option is for developers looking at the internals. It is not something you need day to day. --help and --version are standard on every coreutils tool. If you forget what an option means, run wc --help first, then the manual page.

Counting lines of code is the most common use of wc, but it has its own subtleties. Running wc -l *.py counts the lines of every Python file in one directory, one line per file plus a total at the end. To count a whole project, feed wc the file list — for example with the shell's glob expansion, wc -l src/*.py src/*.js counts two directories at once. Note that the "lines of code" wc reports include blank lines and comment lines. A 500-line file may contain 100 lines of blanks and comments. To count "effective code lines," filter the blanks and comments out first with grep — grep -v '^\s*$' main.py | wc -l drops the blank lines before counting. Line count is not a quality metric, but it is a trend metric: a file that grows from 200 lines to 2,000 lines deserves a look to see whether it should be split.

The difference between wc -l and grep -c deserves its own paragraph, because the two are often mixed up. grep -c counts "the lines that match." wc -l counts "all the newline characters." grep -c 'ERROR' app.log counts the lines containing ERROR; wc -l app.log counts every line. There is one more subtle difference: grep -c counts lines, so even a last line without a newline counts as one line. That means grep -c '' app.log — an empty pattern that matches every line — works around wc -l's missing-trailing-newline problem. The price is speed: grep has to match a pattern on every line, which is much slower than wc. For counting lines in a large file, wc is the first choice. If you also need the last line to count, reach for grep -c ''.

The definition of "word" shows another face on code files. wc -w counts "nonempty sequences of non-whitespace delimited by whitespace," so foo(bar) is one word, x=1 is one word, and import os is two words. Counting the "words" of a code file with wc -w produces a number with almost no meaning: it counts whitespace-separated fragments, not identifiers. The definition was designed for natural-language text. In an English article, words are separated by spaces, and wc -w gives the word count. In code there is no such convention, so the result is only a rough reference. To count identifiers in code, use a dedicated tool such as ctags or a regular expression. That is not wc's job.

A word about wc's history: the manual page credits Paul Rubin and David MacKenzie as authors. It is one of the oldest tools in GNU coreutils, present since the early days of Unix, and its interface has barely changed in decades. The unassuming --debug option hints at something inside: wc has dedicated performance work behind counting newlines. A modern wc does not read a file byte by byte; it reads large blocks and counts newlines with vectorized methods. That is why wc is fast on large files: it does one thing, and it does it to the extreme. Counting a log file of several hundred megabytes usually takes about a second — far faster than any approach that opens the file and counts by hand.

wc pairs well with tee. The tee guide showed how tee sends one input to both the screen and a file. Put wc at the end of the pipeline and you can save the output while counting its lines: some-command | tee out.txt | wc -l. That combination is especially useful for long-running tasks: the output is saved to a file for the record, and the screen tells you how many lines there were. The other way around, wc can also sit in the middle of a pipeline as a "counter" — but note that wc consumes all of its input before printing a number, so it cannot show progress while the upstream command is still running. If you want to watch progress on a long task, use pv or tee, not wc.

One last thing that confuses first-time users: why does the default output have three columns instead of one? Because wc's design philosophy is "one call answers the three most common questions." Line count, word count, and byte count are the three questions you ask most often about a file, and wc puts them in a single output so you do not have to type the command three times. Want only one column? Add -l, -w, or -c. Want all five columns, adding character count and longest line? Add -m and -L. The design has survived from the early days of Unix to today, which suggests it is right: a tool's defaults should answer the most common questions, and options exist for the special cases.

By the way, wc's output is nearly identical across distributions. It is part of GNU coreutils, and coreutils ships with every mainstream Linux distribution. Ubuntu, Debian, CentOS, Arch — the options and output format are basically the same everywhere. Learn it once, use it everywhere. That is also why this series favors these basic tools: they do not chase trends, they do not get discontinued, and they do not change their interfaces. Ten years from now they will look the same. Counting does not go out of date, and it will not be replaced by a newer tool.

One more combination deserves a mention: sort | uniq -c | wc -l. The first two stages sort the lines and count how many times each distinct value appears. The wc -l at the end counts how many distinct values there are. It is the standard way to answer "how many different values are in this column." For example, to count how many distinct IP addresses appear in a log: cut -d' ' -f1 access.log | sort | uniq | wc -l. Each tool does one job — cut slices, sort orders, uniq de-duplicates, wc counts — and the pipeline is the composition. That is the Unix philosophy in one line, and wc is usually the tool at the end of it.

wc is also a writing tool, not just a programming tool. Authors and translators use wc -w to track the length of a draft, and wc -m to count the characters of a Chinese manuscript. The numbers are not the work itself, but they are the most honest progress indicator available: a draft that grows by 500 words a day is moving, and one that stays at the same count for a week is stuck. The same applies to documentation. If your project's README is 300 words, it is probably too short to explain anything. If it is 5,000 words, it is probably too long for anyone to read. Counting does not write the document, but it keeps the document honest.

Finally, wc -c is a quick way to check a file's size without any formatting. ls -l shows the size too, but buried in a line of permissions, owner, and dates. stat shows it with more detail than you usually want. wc -c file prints one number and the file name, nothing else. It is the fastest answer to "how big is this file, exactly." The number is the same as the size ls -l reports, because both read the file's length from the filesystem. The difference is only in presentation: wc gives you the number and gets out of the way.

There is one more property of wc worth knowing: it streams. It reads its input sequentially, in large blocks, and never needs to hold the whole file in memory. That is why it can count a multi-gigabyte log file without breaking a sweat, and why it works just as well on a pipe as on a file. An editor has to load the file to show it. wc only has to pass through it once. When a log file is too big to open, the right first step is often not to open it at all: wc -l huge.log tells you how big the problem is, grep -c 'ERROR' huge.log tells you how much of it is errors, and only then do you decide which slice is worth looking at with head or tail. Counting first, reading second — that order saves a lot of time.

Counting is the smallest possible unit of understanding a file. Before you search it, cut it, sort it, or feed it to a model, you count it. The number tells you what scale of problem you are dealing with, and it costs almost nothing to get. That is why wc has survived for decades with its interface unchanged: it answers the first question, quickly, and gets out of the way.

Boundary note: this guide covers only wc — counting newlines, words, bytes, characters, and maximum line length. It does not cover text search (that is grep's territory), cutting fields out of lines (that is cut's territory), or sorting and de-duplication (that is sort and uniq's territory). Boundaries against published guides: the head-tail guide covers looking at the start and end of a file, the grep guide covers finding lines by content, the cut guide covers cutting fields by column, and the sort+uniq guide covers sorting and counting duplicates after de-duplication. wc is different from all of them: it only counts. It does not search, cut, or sort. The demo in this guide runs in a sandbox: the demo files are generated on the spot by the script with printf (three.txt with three lines of English, nonl.txt with no trailing newline, cjk.txt with two lines of Chinese, a.txt and b.txt with one to two lines each), and the numbers are identical on every run. The demo script ends by printing --- done (work dir removed, no file left) ---, meaning the work directory has been deleted and no file is left behind. A note on locale: the script runs globally under LC_ALL=C, and the -m demonstration deliberately runs alone under LC_ALL=C.UTF-8, as explained in the text and the transcript.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles

Real photograph of a memory module: close-up of the gold contact fingers on a RAM stick (illustrative photo)
Guide
free From Zero: Read the Memory Ledger and Stop Being Fooled by the free Column

Is there enough memory? free answers the question — and the column most people read is the least useful one. This guide runs every command for real on procps-ng free 4.0.4: total, used, free, shared, buff/cache and available, -w, -t, --si, -s sampling, and a live 200MB allocation that shows the kernel handing cache back. The rule to remember: judge memory by available, never by free.

Developer WorkflowTool TipsPerformance
Real photograph of a data center corridor lined with server racks (illustrative photo)
Guide
ss From Zero: Who Holds the Port, Where the Connection Stalls — Read Sockets at a Glance

ss reads the kernel's socket table: who is listening, what state each connection is in, which process holds the socket. This guide runs every command for real on iproute2 ss 6.1.0 — a live http.server on 127.0.0.1:18080, LISTEN vs ESTABLISHED, state filters, ss -s, Unix sockets — with output pasted verbatim.

Developer WorkflowTool TipsDebugging