Need to see how much the log grew overnight. Or verify a CSV has the right number of records before import. Or confirm a config was not truncated during copy. Line count is a quick, useful metric — and Linux has several ways to get it.
wc -l: The Main Tool
wc (word count) counts lines, words, and characters. The -l flag outputs lines only:
wc -l filename.txt
42 filename.txt
To get only the number without the filename — use a redirect:
wc -l < filename.txt
42
The difference matters in scripts: the first form always appends the filename to output, the second returns the number alone.
Multiple Files and a Total
Count lines in several files at once:
wc -l file1.txt file2.txt file3.txt
120 file1.txt
85 file2.txt
204 file3.txt
409 total
wc automatically adds a total line. Useful for a quick summary across a group of logs.
By pattern — all .log files in a directory:
wc -l /var/log/nginx/*.log
Recursively across all files in a directory:
find /var/log -name "*.log" -exec wc -l {} +
grep -c: Lines Matching a Pattern
grep -c counts only lines that match a pattern, not all lines:
grep -c "ERROR" /var/log/app.log
Count lines with today's date:
grep -c "$(date +%Y-%m-%d)" /var/log/syslog
To count all lines through grep — use an empty pattern:
grep -c "" filename.txt
An empty pattern matches every line.
awk: Count With Conditions
awk counts lines through the built-in NR (number of records) variable:
awk 'END {print NR}' filename.txt
Count only non-empty lines:
awk 'NF' filename.txt | wc -l
NF (number of fields) is zero for empty lines — awk skips them.
Count lines longer than 80 characters:
awk 'length > 80 {count++} END {print count}' filename.txt
sed: Quick Counter
sed with -n and the $= command prints the line number of the last line — equivalent to the total line count:
sed -n '$=' filename.txt
Compact and fast, especially on large files.
Count Lines in a Compressed File
Logs are often stored gzipped. Read without decompressing:
zcat /var/log/syslog.1.gz | wc -l
For .bz2:
bzcat file.bz2 | wc -l
Count Lines in Command Output
Count running processes:
ps aux | wc -l
Count open connections on port 80:
ss -tn | grep ':80' | wc -l
Count files in a directory:
ls /var/www/html | wc -l
Edge Case: File Without Trailing Newline
wc -l counts \n characters. If a file ends without a newline, the last line is not counted. This matters for files generated by programs.
Check whether the file ends with a newline:
tail -c 1 filename.txt | xxd
If output is empty — the file ends with \n. If a character appears — there is no trailing newline and wc -l undercounts by one.
Quick Reference
| Task | Command |
|---|---|
| Count lines in a file | wc -l filename |
| Number only (no filename) | wc -l < filename |
| Multiple files with total | wc -l file1 file2 file3 |
| Recursive across directory | find /path -name "*.log" -exec wc -l {} + |
| Lines matching pattern | grep -c "pattern" filename |
| Non-empty lines only | awk 'NF' filename | wc -l |
| Via awk | awk 'END {print NR}' filename |
| Via sed | sed -n '$=' filename |
| In a gzipped file | zcat file.gz | wc -l |
| In command output | ps aux | wc -l |