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.
wccounts 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(-ccounts 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 fileprints three numbers in order: lines, words, and bytes. Despite the name "word count," it counts lines and bytes too.
wc is for. It stands for word count.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.
2. Count Lines Only with -l
Conclusion:
wc -lshows 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:
-wcounts words,-ccounts bytes,-mcounts characters. For multibyte text (like Japanese),-cand-mdiffer.
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).
wc -c printed 10. Is that a bug?-c counts is bytes.-m. Remember that and you won't get confused.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
-mfor characters. Use-cwhen you want the data size - Both count the trailing newline as one unit
5. The Classic Pipe Counting Pattern
Conclusion:
some-command | wc -lcounts output lines = item count.ls | wc -landgrep ... | wc -lare the most common forms.
wc shines. It can also count what it receives through a pipe (|).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.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.
5-2. Count matching lines from a search
# 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, orfind
6. Counting Multiple Files and Totals
Conclusion: Pass multiple files and
wcprints one line each, then atotalline 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
wc -l file...wcopens the file itself, so it labels which file the number came fromwc -l < file... the shellAn interactive program that reads the commands you type and runs them. streams the content in.wcnever learns the filename, so you get the number only
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.
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
-c is bytes and -m is characters. For Japanese the two differ.wc -l. I have that one too.Today's 3-Line Summary
wcprints lines, words, and bytes, in that order from the left- Count Japanese characters with
-m.-ccounts bytes, so the number comes out larger - To count items, use the shape
command | wc -l