Count Words in a Text File With wc, and Why Markdown Skews It
Count words in a text file with wc
Run wc -w notes.txt. The terminal prints the word count and the file name. That number is exact about what wc counts. For a Markdown file, though, it isn't always the number a writer wants. Here's why.
If you write toward a word goal, wc is fast and already on your Mac. It's also literal: it counts tokens in a file, not prose on a page. Below are the options worth knowing, how wc defines a word and a line, bytes versus characters, how Markdown skews the total, and a cleaner way to count.
What wc counts
The four counts you need
By default, wc prints lines, words, bytes, and then the file name, always in that order, according to the macOS man page for wc. When you pass an option, it reports only what you asked for.
wc notes.md # lines, words, bytes, file name
wc -w notes.md # words only
wc -l notes.md # lines only
wc -c notes.md # bytes only
wc -m notes.md # characters onlyTo get the bare number with no file name, feed the file in on standard input:
wc -w < notes.mdWith no file argument, wc reads standard input and prints no file name, so it also works at the end of a pipeline. Wikipedia's wc entry) shows grep -r "example" | wc as a way to preview how large a command's output is without printing it.
How wc defines a word and a line
The man page defines a word as a string of characters delimited by white space. A line is a string of characters delimited by a newline character, and characters after the final newline are not included in the line count.
That second rule matters in practice. If your file ends without a trailing newline, wc -l can report one fewer line than you see in an editor. If the file has a trailing newline, the count matches what you expect.
Several files at once
Pass more than one file and wc prints a line for each, then a cumulative total after the last one:
wc -w chapter-*.mdYou can see at a glance where a long document's words sit.
Bytes versus characters
-c counts bytes and -m counts characters. For plain ASCII text the two match. They differ when the file contains multi-byte Unicode characters, such as an accented letter or an emoji, because one character can take several bytes. Wikipedia notes that newer versions of wc can tell the two apart, and that you pick the behavior with -c or -m.
Locale matters here. The man page says that if the current locale does not support multibyte characters, -m is equivalent to -c. The LANG, LC_ALL, and LC_CTYPE environment variables affect how wc runs. If -m and -c give you the same number on a file you know has accents, check your locale.
One more option: -L prints the length of the longest line. Wikipedia lists it as a GNU extension, and the macOS man page describes it as a non-standard FreeBSD extension compatible with GNU wc. Note that the macOS page we read is dated 2005, so run man wc on your own machine to confirm which options you have.
Why Markdown skews the count
wc splits on white space and nothing else. It does not know what a heading is. Every marker that sits between spaces counts as a word.
Here is a three-line snippet:
# Weekly notes
> Ship the draftA reader sees five words: Weekly, notes, Ship, the, draft. Run wc -w on it and you get seven, because the # and the > each count as one. Both are standalone tokens. In the CommonMark spec, an ATX heading starts with one to six # characters followed by a space, and a block quote starts with >, so in ordinary Markdown those markers almost always have a space after them.
The same thing happens with list markers like - and with the lines that open and close a code fence. If you want the details of each construct, see our Markdown syntax cheat sheet and the guide to ATX headings.
Code is the bigger distortion. Everything inside fenced code blocks is counted as words too. One technical writer reported that wc -w returned 1,548 words for a file full of sample code, and 1,180 after filtering out the code fences with awk first. That is one writer's account of one file, not a rule, but the direction is the point: code-heavy Markdown counts high.
Links are subtler. In [text](https://example.com), there is no space between the link text and the address, so the whole thing is one token if the link text is a single word. A link with several words in its text splits at the spaces, and the URL attaches to the last word. Either way, the URL is counted as part of a word rather than disappearing.
None of this means wc is wrong. It answers a different question: how many whitespace-separated tokens are in this file?
Get a cleaner count with Pandoc
If you want a number closer to what a reader sees, convert the Markdown to plain text first and count that. Pandoc can write plain text as an output format, according to the Pandoc User's Guide, where -t sets the output format and plain is on the list.
pandoc -f markdown -t plain notes.md | wc -wPandoc turns the heading into a line of text and the block quote into plain text, so the # and > markers drop out. If you already use Pandoc to export Markdown, you have it installed.
Treat the result as a good estimate, not a verdict. How Pandoc renders tables, link references, and code can add or remove a few words, so check one of your own files against the number you expect before you rely on it. The most useful habit is consistency: pick one method, use it every time, and your word goal stays comparable from week to week.
If you only count prose, strip the code blocks first and run the same command on what remains.
Doing this with Carets
Counting words is the easy part. Keeping your files plain and portable is what makes a terminal check like wc -w possible at all. Carets is a native notes and code editor for iPhone, iPad, and Mac that works with Markdown, plain text, and code in one place. Your notes stay as plain text files, so wc, Pandoc, and any other command-line tool can read them without an export step.
Syntax highlighting makes the Markdown markers and code fences easy to spot while you write, which helps when you are working out why a count came out high. Files, projects, and tags keep a long document and its chapters organized so a command like wc -w chapter-*.md has something sensible to run against.
Carets is built natively for iOS and Mac rather than as a web wrapper, and it suits writers and developers alike. If you want a single editor for the Markdown you write and the files you check from the terminal, get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
How do I count words in a text file from the terminal?
Run wc -w followed by the file name. wc prints the number of whitespace-delimited words and the file name. Use wc -w < file to get the number alone.
Why does wc count more words than my editor?
wc treats any run of non-whitespace characters as a word, so Markdown markers such as #, >, and - each count, and so does everything inside code blocks. An editor that understands Markdown skips them.
What is the difference between wc -c and wc -m?
-c counts bytes and -m counts characters. They match for plain ASCII text but differ when a file has multi-byte Unicode characters, as long as your locale supports multibyte characters.
Does wc count the last line if the file has no trailing newline?
wc -l counts newline characters, so text after the final newline is not included in the line count. A file with no trailing newline can show one fewer line than you expect.