0

I want to know how many unique IPs have connected to my site. The following worked until logs started rotating:

cut -d' ' -f1 /var/log/apache2/access.log | uniq -d | wc -l 

Is grepping through all the logs the idiomatic solution?

2 Answers 2

2

zgrep is the tool to read rotated logs. It is installed by default on Ubuntu, Mint and Arch.

#!/usr/bin/env bash # Count unique IPs. zgrep -E [0-9]*\.[0-9]*\.[0-9]*\.[0-9] /var/log/apache2/access.log* | cut -d':' -f2 | cut -d' ' -f1 | uniq -d | wc -l 
1

Yes, grep can take multiple files, but so can cut. Since you've already done it that way and it works, then you won't have to change anything other than put a glob in there that can catch all the log files that you need. Something like...

cut -d' ' -f1 /var/log/apache2/access*.log | uniq -d | wc -l 
2
  • 1
    No, the asterisk is after "log": cut -d' ' -f1 /var/log/apache2/access.log* | uniq -d | wc -l Commented Apr 4, 2021 at 17:08
  • 1
    I don't have a crystall ball. :-) Commented Apr 4, 2021 at 17:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.