How to Use the wc Command in Linux to Count Lines, Words, and Bytes

Whether auditing web server access logs, verifying dataset row counts before database imports, or measuring code size across repositories, inspecting text dimensions is a daily task in Linux systems administration. The wc (word count) utility, included as part of GNU Coreutils on virtually every Unix and Linux distribution, provides a fast, lightweight mechanism to calculate lines, words, character counts, and byte sizes across files and terminal streams.

Because wc operates with zero configuration overhead and consumes negligible memory, it is a primary building block in shell pipelines and automation scripts. In this guide, we explore the essential flags of wc, examine single and multi-file auditing, pipe standard input from other commands, and highlight common formatting quirks.

Overview of wc Command Flags

The wc command accepts several primary options that govern which specific metrics are printed:

Command Flag Metric Counted Practical Application
-l (or --lines) Newline characters (\\n) Counting lines in code, CSV rows, or log entries
-w (or --words) Whitespace-delimited words Word counts in documentation, articles, and text files
-c (or --bytes) Raw byte count Checking precise file sizes without metadata overhead
-m (or --chars) Unicode multi-byte characters Counting characters in international UTF-8 text strings
-L (or --max-line-length) Maximum line width Enforcing code styling rules (e.g., 80 or 120 character limits)

When diagnosing running processes or scripts that generate high volumes of terminal output, pairing line counts with pgrep and pkill in Linux helps identify active processes feeding data streams.

Basic Usage on a Single File

Running wc without any arguments against a file prints three default figures followed by the filename:

wc server_access.log

Output format:

  14205   85230 1048576 server_access.log

The columns represent: Lines (14,205), Words (85,230), and Bytes (1,048,576).

Counting Lines with wc -l

The most frequent use case for wc is counting lines. To count records in a CSV or log file without printing word or byte figures:

wc -l customers.csv

Output:

2500 customers.csv

Auditing Multiple Files Simultaneously

You can pass multiple files or glob wildcards to wc. The tool computes statistics for each individual file and prints an aggregate total row at the end:

wc -l *.py

Output:

  120 main.py
   45 config.py
  310 utils.py
  475 total

Using wc in Shell Pipes (stdin)

When wc reads data from a pipe (standard input), it omits the filename from the output, returning pure numerical counts. This makes it ideal for capturing metrics in bash variables:

1. Counting the Number of Files in a Directory

ls -1 /var/log | wc -l

2. Counting Active TCP Network Connections

ss -t | wc -l

3. Extracting Pure Number Output in Shell Scripts

If you pass a file via standard input redirection (<), wc returns only the number without the filename string:

LINE_COUNT=$(wc -l < data.txt)
echo "Total records processed: $LINE_COUNT"

When performing in-depth system troubleshooting, pairing pipe counters with the strace command in Linux allows you to measure exact syscall counts during application execution.

Frequently Asked Questions

Why does wc -l report one less line than my text editor displays?

wc -l counts newline characters (\\n), not visual rows. If the final line of your file does not terminate with a newline character, wc will not count that concluding line.

What is the difference between wc -c and wc -m?

wc -c counts raw bytes on disk, whereas wc -m counts distinct Unicode characters. In ASCII files, both figures match. However, in UTF-8 text containing emoji, accents, or Asian characters (which occupy 2 to 4 bytes per character), -c will return a higher number than -m.

How do I count only non-empty lines in a file?

Pipe the file through grep before running wc:

grep -cve '^\\s*$' myfile.txt

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.