Skip to content

/commands/coreutils/wc-count-lines

wc -l: count the lines in a file

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · ls (GNU coreutils) 8.32updated
tool
GNU coreutils
versions
GNU coreutils 8.x, GNU coreutils 9.x
os
linux, macos, windows

wc -l file prints the number of lines in a file followed by its name; wc -l < file prints only the number. With several files, wc -l *.txt prints one count per file and a total line. wc counts newline characters, so a last line without a trailing newline is not counted. Other options count words (-w), bytes (-c) and characters (-m).

Examples

bash
printf 'alpha\nbeta\ngamma\n' > a.txt
printf 'one\ntwo\n' > b.txt
wc -l a.txt
wc -l < a.txt
wc -l a.txt b.txt

Output

3 a.txt
3
 3 a.txt
 2 b.txt
 5 total

The first command prints the count and the name, the second only the number (useful in scripts: n=$(wc -l < a.txt)), and the third adds a total line. With several files GNU wc pads the counts so the columns line up.

The last line without a newline

bash
printf 'first\nsecond' > no-eol.txt
wc -l < no-eol.txt
grep -c '' no-eol.txt

Output

1
2

The file has two lines on screen, but only one newline, so wc -l reports 1. grep -c '' counts lines that match an empty pattern, which includes the unterminated last line, and reports 2. Files saved by some Windows editors and files produced by printf without a final \n behave this way.

Options

CommandCounts
wc -l fileNewline characters (lines)
wc -w fileWords, separated by whitespace
wc -c fileBytes
wc -m fileCharacters; differs from -c for multibyte UTF-8 text
wc -L fileLength of the longest line (GNU)
wc fileLines, words and bytes, in that order
grep -c 'ERROR' fileLines that match a pattern, instead of piping grep into wc -l
ls | wc -lEntries in a directory (names with newlines count twice)

Count lines in many files

bash
mkdir -p src/lib
printf 'a\nb\n' > src/one.py
printf 'c\nd\ne\n' > src/lib/two.py
find src -name '*.py' -exec wc -l {} + | sort -k2
find src -name '*.py' -exec cat {} + | wc -l

Output

 3 src/lib/two.py
 2 src/one.py
 5 total
5

-exec wc -l {} + passes many file names to one wc call, which avoids Argument list too long from wc -l $(find ...). Piping the contents through cat gives a single number.

Platform differences

GNU wc on Linux and Git Bash prints 3 a.txt; macOS and BSD wc pad every count to eight columns ( 3 a.txt), also when reading standard input. In scripts, strip the spaces with $(( $(wc -l < file) )) or tr -d ' ' so the value compares the same on both systems. On Windows text files with CRLF line endings, wc -l still counts lines correctly because each line ends with \n, but -c includes the \r bytes.

FAQ

Why is wc -l one less than the number of lines in my editor?

wc -l counts newline characters. If the last line has no newline at the end, it is not counted. Editors show it as a line. Add the missing newline, or count with grep -c '' file, which counts the last line even without a newline.

How do I get only the number, without the file name?

Read the file from standard input, wc -l < file, so wc has no name to print. With several files, use cat file1 file2 | wc -l for a single total.

How do I count lines in all files of a project?

find . -name '*.py' -exec wc -l {} + prints one line per file and a total per batch. With thousands of files find runs wc several times, so there can be several total lines; sum the per-file counts instead, or use find ... -exec cat {} + | wc -l for one number.

Sources