wc Command: Counting Lines, Words, and Bytes

wc Command: Counting Lines, Words, and Bytes

What you'll be able to do

  • Count lines, words, bytes, and characters separately with `wc`
  • Tell `-c` (bytes) apart from `-m` (characters) without mixing them up
  • Count items through a pipe, as in `ls | wc -l`

Prerequisites (read these first)

What You'll Learn

  • How to count lines, words, and bytes at once with wc
  • When to use -l / -w / -c / -m
  • How to write the classic pipe counting pattern like ls | wc -l
  • Why beginners get stuck on "bytes and characters differ" and "the last line isn't counted"

Words used here (sorted out first)

  • Line: one line in a file. wc counts lines by counting newline characters.
  • Word: one chunk separated by spaces or newlines. That is not how a human would split Japanese text.
  • Byte: the unit for data size. It is what a file's size is measured in.
  • Character: one character as a person reads it. In Japanese, one character is not one byte. That gap is the main trap here.
  • Character encoding: the rule that turns characters into data. This article assumes UTF-8.

Quick Summary

  • Count everything → wc file
  • Just lines → wc -l
  • Count items → some-command | wc -l
  • Count actual characters (multibyte) → wc -m (-c counts bytes, not characters)

Environment

  • OS: Ubuntu / typical Linux
  • GNU coreutils wc (wc = word count)
  • UTF-8 character encoding assumed

1. What Is wc? Count Everything First

Conclusion: wc file prints three numbers in order: lines, words, and bytes. Despite the name "word count," it counts lines and bytes too.

Lina: Senpai, when I want to know "how many lines is this file?" or "how many words?", do I have to count by hand?
Linny-senpai: That's exactly what wc is for. It stands for word count.
Linny-senpai: The name says words, but it actually reports lines, words, and bytes all at once.
Lina: So the name is a bit narrower than what it does.

Let's prepare a sample file.

$ cat sample.txt
hello world
linux command
penguin gym

Run wc on it directly.

$ wc sample.txt
 3  6 38 sample.txt

What the three numbers mean (left to right)

Position Number Meaning
1st 3 Lines
2nd 6 Words
3rd 38 Bytes

The filename comes at the right end.

Lina: Three numbers at once. I'll get lost about which is which.
Linny-senpai: Just remember the order: lines, words, bytes.
Linny-senpai: In practice you usually want only one of them, like the line count. Let's look at the options next.

2. Count Lines Only with -l

Conclusion: wc -l shows just the line count. It's the go-to for counting log lines or list entries.

$ wc -l sample.txt
3 sample.txt

-l stands for line.

The standard way to ask "how many lines is this log?"

# How many lines piled up in the log
$ wc -l /var/log/syslog

3. Words, Bytes, and Characters: -w / -c / -m

Conclusion: -w counts words, -c counts bytes, -m counts characters. For multibyte text (like Japanese), -c and -m differ.

3-1. Words with -w

$ wc -w sample.txt
6 sample.txt

-w is word. It counts chunks separated by whitespace or newlines as one word each.

Japanese text is not separated by spaces. So for Japanese, the -w result will not match what a person would call a word count.

3-2. Bytes with -c

$ wc -c sample.txt
38 sample.txt

-c looks like it stands for character. What it actually counts is bytes. That is the first pitfall.

3-3. Characters with -m

$ wc -m sample.txt
38 sample.txt

For ASCII-only files, bytes and characters match, because one character is exactly one byte.

The difference shows up with multibyte text. The next section covers it.

Option Mnemonic Counts
-l line Lines
-w word Words
-c Bytes
-m Characters

4. Lina Gets Stuck: Bytes vs Characters for Multibyte Text

Conclusion: In UTF-8 a Japanese character takes 3 bytes. To count characters, use -m (characters), not -c (bytes).

Lina: I counted a file containing "あいう" (3 characters) and wc -c printed 10. Is that a bug?
Linny-senpai: Not a bug. What -c counts is bytes.
Linny-senpai: In UTF-8, one Japanese character takes 3 bytes. So "あいう" is 3 characters × 3 bytes = 9 bytes.
Linny-senpai: Add 1 byte for the trailing newline and you get 10.
Lina: Oh, so the number of characters and the size of the data are two different things. That caught me off guard.
Linny-senpai: Exactly. When you want the count a person would give, use -m. Remember that and you won't get confused.
Lina: Now I know why my number was about three times too big. That clears it up.

Let's see it in action.

$ echo "あいう" > jp.txt
$ wc jp.txt
 1  1 10 jp.txt
# Bytes
$ wc -c jp.txt
10 jp.txt
# Characters (the newline counts as one character too)
$ wc -m jp.txt
4 jp.txt

Beginner trap

  • Asking for a character count of multibyte text with -c, which prints a number roughly 3x larger
  • Use -m for characters. Use -c when you want the data size
  • Both count the trailing newline as one unit

5. The Classic Pipe Counting Pattern

Conclusion: some-command | wc -l counts output lines = item count. ls | wc -l and grep ... | wc -l are the most common forms.

Lina: I can count a file's lines now. But I also want things like "how many files are in this folderA container that organizes files. Same idea as a "folder" on Windows or macOS.?"
Linny-senpai: That's where wc shines. It can also count what it receives through a pipe (|).
Linny-senpai: On screen, ls arranges the names in columns. But when it hands the output to the next command through a pipe, it sends one item per line.
Linny-senpai: So the line count of ls | wc -l is the number of files.

5-1. Count files and directories

$ ls | wc -l
12

When its output goes into a pipe, ls prints one item per line. So counting the lines gives you the item count.

Note that ls skips hidden files, whose names start with .. To include them, write ls -A | wc -l.

# How many lines in the log contain "error"
$ grep "error" app.log | wc -l
27

grep has its own counting option -c. grep -c "error" app.log gives the same result and is shorter.

wc -l, on the other hand, works on the output of any command. It is worth keeping in mind.

Why pipe + wc -l is handy

  • No filename in the output. Just a number, so it plugs straight into further processing
  • Works on the output of any command, such as ls, grep, or find

6. Counting Multiple Files and Totals

Conclusion: Pass multiple files and wc prints one line each, then a total line with the sum.

$ wc -l *.txt
  1 jp.txt
  3 sample.txt
  4 total

The final total is the sum across all files. It is handy for measuring a whole project's line count.

7. Common Beginner Mistakes

Conclusion: Character mismatches come from mixing up -c/-m; pass input via redirection to drop the filename; the last line's count depends on the trailing newline.

7-1. The filename gets in the way

wc -l file prints the filename too. When you want just the number, use the redirection <. It streams the file's contents in as standard input.

# Filename included
$ wc -l sample.txt
3 sample.txt
# Number only (no filename)
$ wc -l < sample.txt
3

How it works

7-2. No trailing newline makes the count look short

wc -l counts newline characters. If the last line has no newline, that line isn't counted.

# A file that doesn't end with a newline
$ printf "a\nb\nc" | wc -l
2

On screen it looks like 3 lines: a, b, and c. But there are only 2 newlines, so the result is 2.

Well-formed text files normally end with a newline. Remember that and this stops being confusing.

7-3. The character count for Japanese is too big

As covered in section 4, -c counts bytes. When you want characters, use -m.

$ wc -m jp.txt   # characters
$ wc -c jp.txt   # bytes (~3x for Japanese)

8. Mini Exercises: Try It Yourself

Conclusion: Three tasks — counting lines, counting items, and counting characters — let you confirm wc basics and pipe usage by hand.

Lina: I've got the knowledge. I want to confirm it hands-on.
Linny-senpai: Great, I prepared three tasks. Try them in your terminal.

Task 1: Count how many lines the following file has.

$ cat << 'EOF' > fruits.txt
apple
banana
orange
grape
EOF
Show hint 1 (direction)

You want the number of lines. Section 2 had an option that counts lines only.

Show hint 2 (command name)

The command is wc. The option is -l.

Show answer
$ wc -l fruits.txt
4 fruits.txt

Task 2: Count how many .txt files are in the current directory.

Show hint 1 (direction)

First list the .txt files. Then count how many lines that listing has. That count is the answer.

Show hint 2 (command name)

Pipe the output of ls *.txt into wc -l.

Show answer
$ ls *.txt | wc -l

The number varies by environment.

Task 3: Count the characters in fruits.txt (not bytes).

Show hint 1 (direction)

You want the number of characters a person would read, not the data size. Recall the split from section 4.

Show hint 2 (command name)

The command is wc. The option is -m. Picking -c would give you bytes.

Show answer
$ wc -m fruits.txt

This file is ASCII text, so -m and -c return the same number here.

Run wc -c fruits.txt as well and compare. Then try the same pair on a file with Japanese text to see the gap appear.

9. Copy-Paste Templates

Conclusion: Keep the common forms — lines, items, characters, totals — within reach.

Handy forms to keep around

# Count everything (lines, words, bytes)
wc file.txt

# Lines only
wc -l file.txt

# Number only (no filename)
wc -l < file.txt

# Count items (count a listing)
ls | wc -l

# Count search matches
grep "keyword" file.txt | wc -l

# Character count (multibyte-aware)
wc -m file.txt

# Byte count (data size estimate)
wc -c file.txt

# Total lines across multiple files
wc -l *.txt

10. Review

Lina: Let me sum up. -c is bytes and -m is characters. For Japanese the two differ.
Linny-senpai: Exactly. In UTF-8 one Japanese character takes 3 bytes.
Lina: And to count items, I pipe a command's output into wc -l. I have that one too.
Linny-senpai: Perfect. Keep in mind that the line count is really a count of newlines.

Today's 3-Line Summary

  1. wc prints lines, words, and bytes, in that order from the left
  2. Count Japanese characters with -m. -c counts bytes, so the number comes out larger
  3. To count items, use the shape command | wc -l

Summary: What to Read Next

Share this article

Next steps