Ask Steves · Daily notes · VPS, 20% off · AI Profit Lab · SEO · Money Hub

14 October 2026 · One command a day

How do I count matching lines in a log file on Linux?

grep -c searches a file for some text and prints how many lines match. It does not print the lines themselves. Use it when you want a quick number from a log, such as how often a page was requested, how many 404s there were or how many hits came from one address.

I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.

The command

grep -c "GET /vps" access.log

grep is the standard Linux tool for searching inside files for text. -c tells it to count the matching lines and print only that number. "GET /vps" is the text to look for. The quotes keep the space inside the pattern, so grep treats it as one search term, not two. In an nginx access log each request line contains the method and the path, so this matches GET requests for paths that start with /vps. access.log is the file to search. On Ubuntu, nginx writes its logs to /var/log/nginx. Only root and the adm group can read them, so in practice you run:

sudo grep -c "GET /vps" /var/log/nginx/access.log

What you will see

$ sudo grep -c "GET /vps" /var/log/nginx/access.log
1342

That single number is how many lines in the file contain GET /vps. Each line in an access log is one request, so this is the number of requests for that path since the log was last rotated. If you give grep more than one file, it prints one count per file in the form filename:count. A result of 0 means nothing matched. In that case grep also exits with status 1, which is useful in scripts.

When to use it

Watch out for

grep matches the text anywhere in the line. This means "GET /vps" also matches /vps-guide, /vpsdeals and /vps/anything. Query strings are a separate issue. A visit to /vps?ref=email is a real hit on the page, but adding a trailing space to the pattern would leave it out. If you want only the page itself, including those query-string visits, allow either a space or a question mark after the path: sudo grep -cE "GET /vps[ ?]" /var/log/nginx/access.log. Before you trust a count, run the search once without -c and look at a few matching lines to check you are counting what you think you are.

Questions people ask

How do I count across older, rotated logs too?

Use zgrep, which reads both gzipped and plain files: sudo zgrep -c "GET /vps" /var/log/nginx/access.log*. It prints one count per file, which you can then add up.

Does grep -c count lines or matches?

It counts lines, so text that appears twice on one line is counted once. To count every occurrence, use grep -o "pattern" file | wc -l instead.

You can only read raw access logs like this on a server you control, where the logs belong to you and are not hidden behind a hosting dashboard. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.

Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.

Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.

GET A VPS, 20% OFF →

Affiliate link.

Related server notes: How do I check if a service is running on Linux? · How do I find the top IP addresses hitting my website on Linux? · How do I read the last 50 lines of a log file on Linux? · All daily notes