wc Command — Counting Lines, Words, Characters, and Bytes in Linux¶
The
wc(Word Count) command is used to count lines, words, characters, and bytes in files or command output. It is one of the most useful Linux utilities for analyzing text files, reports, log files, source code, and command pipelines.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
wccommand - Count lines, words, and characters
- Count bytes in files
- Count multiple files
- Count command output
- Combine
wcwith pipes - Analyze production logs and reports
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–6
Why Learn wc?¶
Imagine you're asked:
- How many users exist on the server?
- How many lines are in a log file?
- How many failed login attempts occurred today?
- How many Kubernetes Pods are running?
Instead of counting manually:
provides the answer instantly.
What is wc?¶
wc stands for:
Word Count
It counts:
- Lines
- Words
- Characters
- Bytes
Syntax:
Sample File¶
Create:
Contents:
Press:
Basic Usage¶
Example output:
Meaning:
| Value | Description |
|---|---|
| 5 | Lines |
| 5 | Words |
| 44 | Bytes |
Count Lines¶
Output:
Count Words¶
Output:
Count Characters¶
Output:
The -m option counts characters, including spaces and newlines where applicable.
Count Bytes¶
Output:
Bytes may differ from characters when using multibyte encodings such as UTF-8.
Count Multiple Files¶
Create:
Count:
Output:
Count Command Output¶
Count files in the current directory.
Count users.
Count running processes.
Count Docker containers.
Count Kubernetes Pods.
Combining with grep¶
Count ERROR messages.
Count failed SSH logins.
Count users using Bash.
Combining with sort and uniq¶
Count unique departments.
Count duplicate IP addresses.
Count Source Code Lines¶
Python files.
Count total lines in Python files.
Common wc Options¶
| Option | Description |
|---|---|
-l | Count lines |
-w | Count words |
-m | Count characters |
-c | Count bytes |
-L | Display longest line length |
Longest Line¶
Display the length of the longest line.
Useful when validating file formats or checking coding standards.
Real Production Examples¶
Count users.
Count log entries.
Count running services.
Count Kubernetes Deployments.
Count mounted filesystems.
Production Perspective¶
The wc command is commonly used to:
- Count log entries
- Measure report sizes
- Count source code files
- Count users and services
- Generate audit reports
- Validate automation output
It is simple, fast, and widely used in shell scripts.
Hands-on Lab¶
Task 1¶
Create:
Contents:
Task 2¶
Count lines.
Task 3¶
Count words.
Task 4¶
Count characters.
Task 5¶
Count bytes.
Task 6¶
Count users.
Task 7¶
Count files.
Task 8¶
Count shell scripts.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
wc | Count everything | Reports |
wc -l | Count lines | Log analysis |
wc -w | Count words | Documents |
wc -m | Count characters | Text validation |
wc -c | Count bytes | File size checks |
wc -L | Longest line | Data validation |
Production Troubleshooting Scenario¶
Scenario
An operations engineer receives a 2 GB application log.
Tasks:
- Count total log entries.
- Count ERROR messages.
- Count WARNING messages.
- Count unique client IPs.
- Count running Kubernetes Pods.
Solutions:
wc -l application.log
grep ERROR application.log | wc -l
grep WARNING application.log | wc -l
cut -d " " -f1 access.log | sort | uniq | wc -l
kubectl get pods | wc -l
Mini Challenge¶
Create:
Perform the following:
- Count total records.
- Count total words.
- Count characters.
- Count unique departments.
- Count employees in Engineering.
- Count total users on your Linux system.
Best Practices¶
- Use
wc -lto count records instead of manual counting. - Combine
wcwithgrepto analyze logs. - Use
wc -Lto validate maximum line lengths. - Use pipes instead of temporary files.
- Include
wcin automation scripts for reporting.
Common Mistakes¶
❌ Confusing characters with bytes.
✅ - -m → Characters - -c → Bytes
They may differ for UTF-8 or other multibyte encodings.
❌ Counting without filtering.
✅ Instead of:
Use:
when you only need ERROR entries.
❌ Forgetting that command headers affect counts.
✅ Example:
The output includes the header line.
To count only Pods:
Interview Questions¶
Beginner¶
- What does
wcstand for? - How do you count lines in a file?
- What is the difference between
-mand-c? - How do you count words?
Intermediate¶
- Explain
wc -L. - How do you count ERROR entries in a log?
- How do you count Linux users?
- How do you count shell scripts in a directory?
Architect Level¶
- How would you estimate the size of a log dataset using
wc? - Why is
wccommonly used in monitoring and reporting scripts? - How would you combine
find,grep, andwcto generate system metrics?
Summary¶
In this lesson, you learned:
- Counting lines
- Counting words
- Counting characters
- Counting bytes
- Counting command output
- Combining
wcwith other Linux utilities - Production reporting techniques
The wc command is a simple yet powerful tool for measuring and summarizing text. It is widely used in shell scripts, automation, monitoring, and system administration.
Key Takeaways¶
wccounts lines, words, characters, and bytes.- Use
-lfor line counts. - Use
-wfor word counts. - Use
-mfor character counts. - Use
-cfor byte counts. - Combine
wcwithgrep,find,cut, andsortfor advanced analysis.
What's Next?¶
paste Command — Merging Files Horizontally in Linux
In the next lesson, you'll learn:
- Combining multiple files side by side
- Changing delimiters
- Processing CSV data
- Working with reports
- Real-world data merging examples