Skip to content
Latchkey

wc -l: Count Lines of Output

wc -l counts the number of newline characters, which equals the line count for newline-terminated text.

wc -l is the workhorse for counting things in CI: matches, changed files, results. The catch is that it counts newlines, not lines.

What it does

wc -l counts newline characters in the input. For normal text ending in a newline this equals the number of lines. Piped after grep or find, it counts results. With multiple files it prints a per-file count and a total.

Common usage

Terminal
grep -c is faster, but: grep ERROR log | wc -l
git diff --name-only | wc -l          # number of changed files
find . -name '*.py' | wc -l           # count files
wc -l < file.txt                      # count only, no filename

Options

FlagWhat it does
-lCount newlines (lines)
-wCount words
-cCount bytes
-mCount characters
-LLength of the longest line

In CI

Use wc -l < file (redirect) instead of wc -l file when you want only the number, since the file form also prints the filename and breaks numeric comparisons. To count matches, grep -c is usually clearer than grep | wc -l.

Common errors in CI

wc -l counts newlines, so a final line with no trailing newline is not counted, undercounting by one; output from printf without a trailing \n is a common culprit. wc -l file prints leading spaces and the filename, which breaks if test "$(wc -l file)" -gt 0; redirect with < to get a bare number.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →