Skip to main content

Syntax

wc [OPTIONS] [FILE...]

Description

The wc (word count) command counts lines, words, and bytes in text input. It’s a fundamental tool for analyzing file sizes, counting records, and understanding text structure. By default, wc displays all three counts. You can specify flags to show only specific counts. Nash’s wc operates entirely in-memory and supports reading from files or standard input.

Options

-l
flag
Count lines only. Output the number of newline characters.
-w
flag
Count words only. Words are whitespace-separated tokens.
-c
flag
Count bytes only. Output the total number of bytes (characters).
When no options are specified, wc displays lines, words, and bytes (in that order). When multiple options are specified, counts appear in the order: lines, words, bytes.

Examples

Default output (all counts)

echo -e "hello world\nfoo bar" | wc
       2        4       20
Output format: lines words bytes
  • 2 lines
  • 4 words (hello, world, foo, bar)
  • 20 bytes

Count lines only

echo -e "line 1\nline 2\nline 3" | wc -l
       3

Count words only

echo "the quick brown fox" | wc -w
       4

Count bytes only

echo "hello" | wc -c
       6
Note: Includes the newline character added by echo.

Multiple flags

echo -e "one two\nthree four" | wc -l -w
       2        4
Shows lines and words (in that order).

Count file contents

echo -e "line 1\nline 2" > test.txt
wc test.txt
       2        4       14

Pipeline Examples

Count matches

grep ERROR app.log | wc -l
Counts how many lines contain “ERROR”.

Count unique values

cat data.txt | sort -u | wc -l
Counts unique lines in file.

Words in a document

cat article.txt | wc -w
Total word count.

File size approximation

cat large-file.txt | wc -c
Shows byte size.

Count after filtering

ls | grep "\.txt$" | wc -l
Counts .txt files in directory.

Average line length

BYTES=$(cat file.txt | wc -c)
LINES=$(cat file.txt | wc -l)
echo "Average: $((BYTES / LINES)) bytes per line"

Practical Use Cases

Verify CSV row count

cat data.csv | wc -l
Quick check of record count.

Check if file is empty

test $(cat file.txt | wc -l) -eq 0 && echo "Empty file"

Count log entries by level

echo "Errors: $(grep ERROR app.log | wc -l)"
echo "Warnings: $(grep WARN app.log | wc -l)"
echo "Info: $(grep INFO app.log | wc -l)"

Estimate reading time

WORDS=$(cat article.txt | wc -w)
MINS=$((WORDS / 200))
echo "Estimated reading time: $MINS minutes"

Monitor log growth

echo "Log size: $(cat app.log | wc -l) lines"

Count code lines

cat *.js | grep -v "^$" | grep -v "^[ ]*//" | wc -l
Non-empty, non-comment lines.

Data validation

EXPECTED=1000
ACTUAL=$(cat output.txt | wc -l)
test $ACTUAL -eq $EXPECTED && echo "OK" || echo "Mismatch: $ACTUAL rows"

Advanced Examples

Compare file sizes

echo "file1.txt: $(cat file1.txt | wc -c) bytes"
echo "file2.txt: $(cat file2.txt | wc -c) bytes"

Count fields in CSV

LINES=$(cat data.csv | wc -l)
FIELDS=$(head -1 data.csv | tr ',' '\n' | wc -l)
echo "$LINES rows × $FIELDS columns"

Average words per line

LINES=$(cat doc.txt | wc -l)
WORDS=$(cat doc.txt | wc -w)
echo "Average: $((WORDS / LINES)) words/line"

Multiple files

wc file1.txt file2.txt file3.txt
Shows counts for each file separately.

Conditional processing

if [ $(cat errors.log | wc -l) -gt 100 ]; then
  echo "Too many errors!"
fi

Output Format

All counts are right-aligned in 8-character fields:
echo "test" | wc
       1        1        5
Format: %8d for each count.

Single count format

echo "test" | wc -l
       1

Multiple counts format

echo "test" | wc -l -c
       1        5
Order is always: lines, words, bytes.

Understanding Counts

Number of newline characters (\n).
echo -e "a\nb\nc" | wc -l
Output: 3
Files without a trailing newline may count one fewer line than expected.

Common Patterns

Count pipeline results

command | wc -l
Standard way to count output lines.

Check file emptiness

test $(cat file.txt | wc -l) -gt 0 && echo "Has content"

Sum counts across files

wc -l *.txt
Shows individual and total counts.

Store count in variable

COUNT=$(cat data.txt | wc -l)
echo "Found $COUNT records"

Percentage calculation

TOTAL=$(cat all.txt | wc -l)
FILTERED=$(cat all.txt | grep pattern | wc -l)
PERCENT=$((FILTERED * 100 / TOTAL))
echo "$PERCENT% match"

Tips

Newline counting: wc -l counts newline characters. A file without a trailing newline may show one fewer line than you expect.

Empty input

echo -n "" | wc -l  # Output: 0 (no newline)
echo "" | wc -l     # Output: 1 (one newline)

Word definition

Words are separated by any whitespace: spaces, tabs, newlines.
echo -e "one\ttwo\nthree" | wc -w  # Output: 3

Byte vs character

For ASCII text, bytes = characters. For Unicode, may differ (not applicable in Nash’s VFS).

Combining with other commands

Always use wc at the end of pipelines:
grep pattern file | sort | uniq | wc -l

Performance

Nash’s wc loads entire input into memory for counting. This is efficient for typical text files in the VFS but consider the memory footprint for very large datasets.
  • grep - Filter lines before counting
  • sort - Organize before counting
  • uniq - Remove duplicates before counting
  • cut - Extract fields for counting
  • head - Limit lines
  • tail - View last lines

Build docs developers (and LLMs) love