I'm on Ubuntu and I typed cat .bash_history | grep git
and it returned
Binary file (standard input) matches
My bash_history
does exist and there are many lines in it that starts with git
.
What caused it to display this error and how can I fix it?
5 Answers 5
You can use grep -a 'pattern'
.
from man grep page:
-a, --text
Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
-
This has helped me when using the
-z
flag to match across several lines.stragu– stragu2018年04月24日 07:51:17 +00:00Commented Apr 24, 2018 at 7:51 -
3It has worked for me, but still weird because my file isn't a binary file. [grid@serverdg2 trace]$ file listener.log listener.log: dataAstora– Astora2020年12月01日 03:42:35 +00:00Commented Dec 1, 2020 at 3:42
-
2It is enough, if your file has a single non-decodable character, and grep will fail (if not used with -a)ivan– ivan2022年05月05日 11:43:18 +00:00Commented May 5, 2022 at 11:43
Presumably the file .bash_history
starts with non-text data, hence grep
is treating the file as binary. This is confirmed by the file .bash_history
output:
.bash_history: data
You can read a few bytes from start to have a conforming view:
head -c1K .bash_history
Here I am reading first 1 KiB.
You can pipe the STDOUT to hexdump
/od
or similar.
As a side note, grep
takes filename(s) as argument, so cat
is useless here; try this:
grep git .bash_history
-
1I'm still not sure how to solve the grep issue,
head -c1k .bash_history
read the first 38 lines of my .bash_history file. Everything was readableanswerSeeker– answerSeeker2017年01月08日 05:22:44 +00:00Commented Jan 8, 2017 at 5:22 -
6@TatakaiWasumi Whats the output of
grep -a git .bash_history
?heemayl– heemayl2017年01月08日 05:29:51 +00:00Commented Jan 8, 2017 at 5:29 -
2That worked! I got everything I wanted from it. What does
-a
do?answerSeeker– answerSeeker2017年01月08日 05:31:56 +00:00Commented Jan 8, 2017 at 5:31 -
10@TatakaiWasumi
-a
makesgrep
to treat the file as binary.heemayl– heemayl2017年01月08日 05:34:08 +00:00Commented Jan 8, 2017 at 5:34 -
9
-a
makegrep
process a binary file as if it were text.lashgar– lashgar2018年04月06日 23:01:00 +00:00Commented Apr 6, 2018 at 23:01
This can be caused by null bytes in your bash history. Removing lines with null characters may resolve the issue. You can check for them using grep's Perl-regexp mode:
grep -Pa '\x00' .bash_history
This post has suggestions for non-unix systems.
-
1This actually helped me find out why I was having the same problem.DUO Labs– DUO Labs2021年09月23日 19:16:01 +00:00Commented Sep 23, 2021 at 19:16
-
Plus one from me too.L1ttl3J1m– L1ttl3J1m2022年03月31日 00:43:06 +00:00Commented Mar 31, 2022 at 0:43
-
same as me, tooCherrymelon– Cherrymelon2023年10月10日 02:15:39 +00:00Commented Oct 10, 2023 at 2:15
I had the same problem when I want to grep my .bash_history
. (Little Note: I renamed my history, so that a new one was created. This new history was not treated as a binary.)
In @heemayls answer it is stated, that grep
takes filenames and cat
would be useless. This is not entirely true.
From grep
s man page:
If no files are specified, or if the file "-" is given, grep searches standard input.
So you could use cat
and pipe it to grep
. However this solves not the problem that .bash_history
is treated as a binary. The only right thing is to use grep -a
(Like in the answer from @AK_) whether you grep
the history directly or with cat
and a pipe.
cat .bash_history | grep -a git
or
grep -a git .bash_history
Error is due to the data in the file being binary, you can use strings command to see the human readable (i.e. strings
) part which you would normally search using grep
strings data | grep -i whatever
-
1This does not address the first part of the question, i.e. "what causes the issue".2020年03月24日 09:47:11 +00:00Commented Mar 24, 2020 at 9:47
file .bash_history
(file ~/.bash_history
)?.bash_history: data