Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I count lines, words, or characters in a text file from the command line?

Quick answer

With GNU wc, use wc -l file.txt for newline characters, wc -w file.txt for whitespace-separated words, wc -m file.txt for characters, or wc -c file.txt for bytes. Combine options for several counts at once. Note that -l may not count a final line without a newline, and -m depends on the current locale. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a text file and want a quick command-line count. Which commands should I use for lines, words, and characters?

Geez

For GNU wc, replace sample.txt below with your filename:

  1. wc -l sample.txt counts newline characters.
  2. wc -w sample.txt counts words, defined as nonempty sequences separated by whitespace.
  3. wc -m sample.txt counts characters according to the current locale.
  4. wc -c sample.txt counts bytes.

To request all four counts at once, run wc -l -w -m -c sample.txt. GNU wc prints the counts followed by the filename. These details describe GNU wc; other systems or command versions may differ. [S1]

Follow-up question

Could you show me a small example and how to read the result?

Geez

Assuming a shell where printf is available, create a small example file with two newline-terminated lines:

printf 'red fox\nblue\n' > sample.txt

Then run:

wc -l -w -m -c sample.txt

For GNU wc, the counts should correspond to 2 newline characters, 3 whitespace-separated words, 13 characters, and 13 bytes. The text is ASCII, so its character and byte counts match in this example. The displayed output also includes sample.txt; spacing can vary. [S1]

Follow-up question

Why might wc -l give one fewer line than I see in a text editor? And is -m always the character count I mean?

Geez

wc -l counts newline characters, not every line-shaped piece of text. If a file has a final line without a newline, GNU wc -l does not count that trailing partial line, so the result can be lower than the number of lines you informally see. [S1]

-m counts characters according to the current locale, and GNU wc does not count encoding errors. It is not interchangeable with the byte count from -c. [S1] If you need a particular application’s definition of “character,” check that definition before comparing results. The rules described here are for GNU wc, not every platform or application.

Follow-up question

What’s a practical way to choose the right count when I’m checking a file?

Geez

Start by deciding what you need: newline characters, whitespace-separated words, locale-based characters, or bytes. For an overview, use wc -l -w -m -c sample.txt; for one number, use the matching option alone. If the line total seems unexpected, check whether the final line ends with a newline. If character and byte totals differ, verify the locale and encoding relevant to your task rather than assuming one count is universally correct. [S1]

A reusable template is:

wc -l -w -m -c [path-to-file]

Replace the bracketed placeholder with the file’s path. This command is for GNU wc; check your platform’s documentation if its behavior or options matter.

A practical checklist

  • Choose whether you need newline, word, character, or byte counts.

  • Run the matching wc option, or combine options for an overview.

  • Use -c when you need bytes rather than a locale-based character count.

  • If the line total looks low, check whether the final line has a newline.

  • Confirm your platform’s wc behavior if results must match another tool.

A few more useful answers

Does `wc -l` count a final line that has no newline?

GNU wc -l counts newline characters, so it does not count a trailing partial line without a newline as another newline. [S1]

What is the difference between `wc -m` and `wc -c`?

-m reports characters according to the current locale; -c reports bytes. GNU wc does not count encoding errors in its character count. [S1]

Does `wc -w` use the same word definition as every word processor?

Not necessarily. GNU wc treats a word as a nonempty sequence separated by whitespace, and whitespace interpretation is affected by locale. [S1]

Sources

References used for this guide.

  1. [S1] GNU Coreutils 9.11: wc — Print newline, word, and byte counts
    gnu.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work