0

I'm trying to print all root accesing attempts from /var/log/auth.log with grep. But it seems like grep isn't processing the whole file, instead grep is stopping at some point:

> less /var/log/auth.log | grep Accepted
Apr 10 08:32:43 sshd[16845]: Accepted password for root from ... port 49061 ssh2
Apr 10 09:42:38 sshd[15451]: Accepted password for root from ... port 48990 ssh2
Apr 10 10:11:17 sshd[21592]: Accepted password for root from ... port 48686 ssh2
Apr 10 12:09:03 sshd[5757]: Accepted password for root from ... port 62535 ssh2
Apr 11 18:12:12 sshd[1804]: Accepted password for root from ... port 40765 ssh2
Apr 11 18:12:30 sshd[1884]: Accepted password for root from ... port 40808 ssh2
Apr 11 18:13:46 sshd[2063]: Accepted password for root from ... port 40735 ssh2
Apr 11 19:58:44 sshd[17103]: Accepted password for root from ... port 40684 ssh2
Apr 11 20:48:06 sshd[24583]: Accepted password for root from ... port 40466 ssh2
Binary file (standard input) matches

As we can see, there are only entries until the Apr 11th. So if I try the following command:

> less /var/log/auth.log | grep "Apr 12"
Binary file (standard input) matches

Nothing gets printed. But in the file /var/log/auth.log the "Apr 12" entries are present as we can see if I run this command (I also proved it by printing the whole file with cat):

> less /var/log/auth.log | grep -c "Apr 12"
4297

I don't understand what I am doing wrong with my first grep command.

  • 4
    Why are you using less? Just grep "Apr 12" /var/log/auth.log – Jesse_b Apr 12 '18 at 16:29
  • Sounds like you have some non-ASCII data in the log. – Jeff Schaller Apr 12 '18 at 16:33
  • @Jesse_b I know, I was playing arround for testing purporses :) – JulianH Apr 12 '18 at 17:12
4

To force grep to treat files with binary content as ASCII, use its -a flag:

grep -a 'Apr 12' /var/log/auth.log

This is a non-standard option but is implemented in GNU grep and in the grep on OpenBSD (and probably the other BSD's as well).

When the data that is processed by GNU grep contains nul bytes, or when it is improperly encoded for the current locale, grep will treat it as binary. With -a or --binary-files=text, GNU grep will be forced to treat such data as text. See the documentation for the --binary-files option in the GNU grep manual.

grep normally refuses to output binary data as it may have "have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands." (quote from the manual).

  • Thanks first, your hint solved my problem. What's graps beheavior in case of binary content is present? How grep notice there is binary data present? – JulianH Apr 12 '18 at 17:08
  • @JulianH See updated answer. – Kusalananda Apr 12 '18 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.