Quick take: The wc command counts lines, words, and characters. wc -l file counts lines, wc -w counts words, and piping into wc (ls | wc -l) counts the output of another command — a hugely useful trick.
Introduction
The wc command (word count) reports the number of lines, words, characters, and bytes in its input. Its real power comes in pipelines, where it counts the output of other commands — answering questions like “how many files are here?” or “how many errors are in this log?”.
This guide covers counting within files and the pipeline patterns that make wc one of the most-used helpers on the command line.
Syntax
The basic syntax of the wc command is:
wc [OPTIONS] [FILE...]Common Options and Parameters
The most useful options and parameters for the wc command:
| Option | Description |
|---|---|
| -l | Count lines. |
| -w | Count words. |
| -c | Count bytes. |
| -m | Count characters (matters for multibyte text). |
| -L | Print the length of the longest line. |
Practical Examples
Real wc commands you can run today:
# Count lines, words, and bytes
wc report.txt
# Count just the lines
wc -l report.txt
# Count words
wc -w essay.txt
# Count files in a directory
ls -1 | wc -l
# Count matching lines from grep
grep -c ERROR app.log # or: grep ERROR app.log | wc -l
# Count lines across several files
wc -l *.csvTips and Best Practices
command | wc -lis the universal “how many?” pattern — count files, matches, processes, anything line-based.- When counting files, prefer
ls -1 | wc -l(one per line) for an accurate count. wc -lon many files prints a per-file count plus a total — handy for a quick size overview.
Final Thoughts
wc turns any stream of text into a number. On its own it summarises a file; in a pipeline it counts the output of any command, which is why | wc -l appears constantly in real-world shell work. Pair it with grep, ls, and find to answer “how many?” in a single line.
FAQ: wc Command in Linux
How do I count the number of lines in a file?+
Use wc -l filename. It prints the line count. To count words use wc -w and to count characters use wc -m.
How do I count files in a directory?+
Pipe a one-per-line listing into wc: ls -1 | wc -l. For a recursive count of files, use find . -type f | wc -l.
How do I count how many times a pattern appears?+
Use grep -c pattern file for matching lines, or grep pattern file | wc -l. Note grep -c counts lines, not total occurrences.
What does wc show by default?+
With no options, wc prints three numbers — lines, words, and bytes — followed by the filename. Use -l, -w, or -c to show just one.
How do I find the longest line in a file?+
Use wc -L file, which prints the length of the longest line in characters.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support