Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running a grep on a file that is about 13G in size. It is returning

Binary file file.xml matches

I was not expecting this, I thought it would return each line with my string so that I could run the following,

grep "searchString" ./file.xml | wc -l 

and return a count of all the occurrences of my searchString in my large file.

share|improve this question
Out of interest, what does file file.xml return? – EightBitTony Jul 9 '13 at 13:18
file.xml: XML document text – Dan Ciborowski - MSFT Jul 9 '13 at 13:22
up vote 12 down vote accepted

It looks like grep thinks your XML file is binary rather than text.

If you want to force grep to treat your file as text regardless of the contents, you can use its --text switch (assuming GNU grep), like so:

grep --text "searchString" ./file.xml | wc -l

Note that if all you want is to count matches, it's probably better to use grep --count rather than piping through wc -l, saving you a pipe and process invocation.

share|improve this answer
Solved. Thanks! – Dan Ciborowski - MSFT Jul 9 '13 at 13:21
@MichaelKjörling grep --count will also return number of lines. From man: -c, --count Suppress normal output; instead print a count of matching lines for each input file. – rush Jul 9 '13 at 13:31
@rush Argh, you're right. Sorry, never mind that then. – Michael Kjörling Jul 9 '13 at 13:34

Looks like there are some uncommon symbols in the beginning of your file and grep detects it as binary. You can try --binary-files=text option.

grep --binary-files=text "searchString" file.xml | wc -l

From man page:

          If the first few bytes of a file indicate that the file contains
          binary  data, assume that the file is of type TYPE.  By default,
          TYPE is binary, and grep  normally  outputs  either  a  one-line
          message  saying  that  a  binary  file matches, or no message if
          there is no match.  If TYPE is without-match, grep assumes  that
          a  binary  file  does  not  match;  this is equivalent to the -I
          option.  If TYPE is text, grep processes a binary file as if  it
          were  text;  this is equivalent to the -a option.  Warning: grep
          --binary-files=text might output binary garbage, which can  have
          nasty  side  effects  if  the  output  is  a terminal and if the
          terminal driver interprets some of it as commands.
share|improve this answer
Or near-equivalently grep -a -c 'searchString' file.xml and avoid the superfluous wc -l. – tripleee Jul 9 '13 at 13:26
Just DOS carriage returns are enough to turn a file into "binary". – tripleee Jul 9 '13 at 13:40
@tripleee Not to be a nit, but I thought I already said that :) – Michael Kjörling Jul 9 '13 at 13:40
@MikaelKjörling: After I posted my comment, yes. Yours is now the better answer so you got my vote. – tripleee Jul 9 '13 at 13:42

It looks like you have a mistake when using ./file.xml. If you try:

grep "searchString" file.xml | wc -l

Does it have any problems?

share|improve this answer
file.xml and ./file.xml refer to exactly the same file, with the additional safety net that in the latter case, the real name of file.xml can safely begin with e.g. -. – Michael Kjörling Jul 9 '13 at 13:09
not going to vote down but above comment is right no difference between what you have and what I tried. – Dan Ciborowski - MSFT Jul 9 '13 at 13:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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