/commands/coreutils/wc-count-lines
wc -l: count the lines in a file
- 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
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.txtOutput
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
printf 'first\nsecond' > no-eol.txt
wc -l < no-eol.txt
grep -c '' no-eol.txtOutput
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
| Command | Counts |
|---|---|
| wc -l file | Newline characters (lines) |
| wc -w file | Words, separated by whitespace |
| wc -c file | Bytes |
| wc -m file | Characters; differs from -c for multibyte UTF-8 text |
| wc -L file | Length of the longest line (GNU) |
| wc file | Lines, words and bytes, in that order |
| grep -c 'ERROR' file | Lines that match a pattern, instead of piping grep into wc -l |
| ls | wc -l | Entries in a directory (names with newlines count twice) |
Count lines in many files
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 -lOutput
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.