How do I count matching lines?
grep -c ' 500 ' /var/log/nginx/access.log
-c prints the number of lines that match, not the number of matches. A line with the pattern three times counts once. For log analysis that is usually right: one request, one line, one count.
How do I count every occurrence?
Use -o to print each match on its own line, then count lines:
grep -o 'ERROR' /var/log/app/worker.log | wc -l grep -oi 'timeout' /var/log/app/*.log | wc -l
This counts a line with three matches as three. -i ignores case. The difference matters for things like counting how many times a word appears in a document, or how many IDs appear in a JSON line.
How do I count per file?
grep -c 'Failed password' /var/log/auth.log* grep -rc 'TODO' /srv/app/src --include='*.php' | grep -v ':0$' | sort -t: -k2 -n
With several files, -c prints file:count for each. The second line searches a tree recursively, drops files with zero matches and sorts by count, giving a ranked list. To total across all files, add the numbers: grep -rh 'TODO' /srv/app/src | wc -l, where -h drops file names.
How do I count lines that do not match?
grep -vc '^#' /etc/ssh/sshd_config grep -vcE '^\s*(#|$)' /etc/nginx/nginx.conf
-v inverts the match. The second command counts lines that are neither comments nor blank, a quick measure of how much real configuration a file holds.
How do I count by value?
Extract the value with -o, then let sort | uniq -c group it:
grep -oE '" [0-9]{3} ' /var/log/nginx/access.log | sort | uniq -c | sort -rn
grep -oP 'Failed password for (invalid user )?\K\S+' /var/log/auth.log | sort | uniq -c | sort -rn | head
The first gives a count of each HTTP status code. The second lists the user names that failed SSH logins most often, using Perl regex with \K to keep only the part after it. On a box with password logins enabled, the list is usually long, which is a good argument for key-only access, explained in Harden SSH on Linux.
How do I count over a time window?
grep -c '03/Dec/2026:14:' /var/log/nginx/access.log # requests in the 14:00 hour journalctl -u ssh --since '1 hour ago' --no-pager | grep -c 'Accepted publickey'
For logs with timestamps in each line, matching the time prefix is fast and simple. For the journal, filter by time first with --since, then count. Hourly counts help spot traffic spikes that might eat into the monthly outgoing allowance.
What traps should I watch for?
| Trap | Fix |
|---|---|
| -c counts lines, not matches | Use -o ... | wc -l |
| Rotated logs are compressed | Use zgrep -c on .gz files |
| Pattern matches more than intended | Add -w for whole words or anchor with ^ and $ |
| Binary files report "matches" | Add -a or exclude them |
| Large logs are slow | Use LC_ALL=C grep or -F for fixed strings |
LC_ALL=C makes grep skip locale handling, which can speed it up several times on big ASCII logs. -F treats the pattern as plain text, avoiding regex work entirely. On the NVMe drives in every model from S1 up, grep through gigabytes of logs takes seconds.
Where do I go from here?
Searching for several patterns at once is covered in Grep for multiple strings, skipping files and lines in Exclude with grep, and the full command in The grep command.
How do I count unique values instead of hits?
Add sort -u before counting. grep -oE '^[0-9.]+' access.log | sort -u | wc -l counts distinct client addresses in a log rather than requests, a quick estimate of how many visitors you served.
Keep Reading
The grep command
grep from basics to power use: case, whole words, line numbers, context, recursion, regex flavors, only-matching output and speed tips.
OpenGrep for multiple strings
Match any of several patterns, all of them on one line, whole words from a list or strings in a set order, with grep and small pipelines.
OpenExclude with grep
Leave things out of grep results: invert matches, skip files and folders by pattern, ignore binaries and comments, and filter noisy log lines.
OpenHarden SSH on Linux
A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.
OpenFAQ
Quick Answers
The number of matching lines, not the number of matches on those lines.
Use grep -o followed by the word and pipe the output to wc -l.
Use zgrep -c on .gz files.
Combine -v and -c, as in grep -vc pattern file.
Extract values with grep -o, then pipe through sort | uniq -c | sort -rn.