Skip to content

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

Linux Mastery → Module 3: Text Processing → Lesson 7

Difficulty: Beginner → Intermediate

Reading Time: 25 Minutes

Course Progress

Course: Linux Mastery

Module: Text Processing

Lesson: 7 of 18


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand the wc command
  • Count lines, words, and characters
  • Count bytes in files
  • Count multiple files
  • Count command output
  • Combine wc with 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:

wc

provides the answer instantly.


What is wc?

wc stands for:

Word Count

It counts:

  • Lines
  • Words
  • Characters
  • Bytes

Syntax:

wc [OPTIONS] FILE

Sample File

Create:

cat > linux.txt

Contents:

Linux

Docker

Kubernetes

Terraform

Ansible

Press:

Ctrl + D

Basic Usage

wc linux.txt

Example output:

5 5 44 linux.txt

Meaning:

Value Description
5 Lines
5 Words
44 Bytes

Count Lines

wc -l linux.txt

Output:

5 linux.txt

Count Words

wc -w linux.txt

Output:

5 linux.txt

Count Characters

wc -m linux.txt

Output:

39 linux.txt

The -m option counts characters, including spaces and newlines where applicable.


Count Bytes

wc -c linux.txt

Output:

44 linux.txt

Bytes may differ from characters when using multibyte encodings such as UTF-8.


Count Multiple Files

Create:

touch file1.txt

touch file2.txt

Count:

wc file1.txt file2.txt

Output:

0 0 0 file1.txt

0 0 0 file2.txt

0 0 0 total

Count Command Output

Count files in the current directory.

ls | wc -l

Count users.

cut -d ":" -f1 /etc/passwd | wc -l

Count running processes.

ps -ef | wc -l

Count Docker containers.

docker ps | wc -l

Count Kubernetes Pods.

kubectl get pods | wc -l

Combining with grep

Count ERROR messages.

grep ERROR application.log | wc -l

Count failed SSH logins.

grep "Failed password" /var/log/auth.log | wc -l

Count users using Bash.

grep "/bin/bash" /etc/passwd | wc -l

Combining with sort and uniq

Count unique departments.

cut -d "," -f2 employees.csv | sort | uniq | wc -l

Count duplicate IP addresses.

cut -d " " -f1 access.log | sort | uniq -c | wc -l

Count Source Code Lines

Python files.

find . -name "*.py" | wc -l

Count total lines in Python files.

find . -name "*.py" -exec cat {} + | wc -l

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.

wc -L linux.txt

Useful when validating file formats or checking coding standards.


Real Production Examples

Count users.

cut -d ":" -f1 /etc/passwd | wc -l

Count log entries.

grep ERROR app.log | wc -l

Count running services.

systemctl list-units | wc -l

Count Kubernetes Deployments.

kubectl get deployments | wc -l

Count mounted filesystems.

mount | wc -l

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:

cat > commands.txt

Contents:

Linux

Docker

Kubernetes

Terraform

Ansible

Task 2

Count lines.

wc -l commands.txt

Task 3

Count words.

wc -w commands.txt

Task 4

Count characters.

wc -m commands.txt

Task 5

Count bytes.

wc -c commands.txt

Task 6

Count users.

cut -d ":" -f1 /etc/passwd | wc -l

Task 7

Count files.

ls | wc -l

Task 8

Count shell scripts.

find . -name "*.sh" | wc -l

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:

  1. Count total log entries.
  2. Count ERROR messages.
  3. Count WARNING messages.
  4. Count unique client IPs.
  5. 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:

employees.csv
Alice,Engineering

Bob,HR

Charlie,Finance

David,Engineering

Eve,HR

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 -l to count records instead of manual counting.
  • Combine wc with grep to analyze logs.
  • Use wc -L to validate maximum line lengths.
  • Use pipes instead of temporary files.
  • Include wc in 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:

wc -l application.log

Use:

grep ERROR application.log | wc -l

when you only need ERROR entries.


❌ Forgetting that command headers affect counts.

✅ Example:

kubectl get pods | wc -l

The output includes the header line.

To count only Pods:

kubectl get pods --no-headers | wc -l

Interview Questions

Beginner

  1. What does wc stand for?
  2. How do you count lines in a file?
  3. What is the difference between -m and -c?
  4. How do you count words?

Intermediate

  1. Explain wc -L.
  2. How do you count ERROR entries in a log?
  3. How do you count Linux users?
  4. How do you count shell scripts in a directory?

Architect Level

  1. How would you estimate the size of a log dataset using wc?
  2. Why is wc commonly used in monitoring and reporting scripts?
  3. How would you combine find, grep, and wc to generate system metrics?

Summary

In this lesson, you learned:

  • Counting lines
  • Counting words
  • Counting characters
  • Counting bytes
  • Counting command output
  • Combining wc with 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

  • wc counts lines, words, characters, and bytes.
  • Use -l for line counts.
  • Use -w for word counts.
  • Use -m for character counts.
  • Use -c for byte counts.
  • Combine wc with grep, find, cut, and sort for 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