Home > Net >  When using grep the message "Binary file (standard input) matches" appears
When using grep the message "Binary file (standard input) matches" appears

Time:01-03

I was using a temporal file to find all the files that use a certain extension and after executing all the files I wanted appeared, but with the message Binary file (standard input) matches. What should I do to fix it when $i is the directory that is inspected and $1 the extension?

tempfile=$(mktemp)
ls "$i" | grep -v -e '^total` -e *.$1 > $tempfile
cat $tempfile

Coincidencia en el archivo binario Ejercicio bases practica.odb
Coincidencia en el archivo binario Ejercicio PR4.odb

I also tried other ways to delete all the other lines from the temporaly file and this one was the only one that seemed, or was close being correct. I tried to use -a as many other cases recomended but it didn't work.

CodePudding user response:

To list all files in Bash you can use find command for example find . -name \*.txt -print to list all files with .txt extension. It will search just using filename (not content).

CodePudding user response:

Trying to convict the suspect using circumstantial evidence:

(1) You grep in the stdin (which is the result of the ls operation) for lines staring with total

(2) You get two matching lines, none of them seem do contain the text total anywhere.

(3) You get a warning about stdin being binary.

This means (since grep does not lie) that you indeed got lines starting with total, but when you do a cat, you can't see this word displayed.

This means in turn that there must be somewhere a carriage return character inside the matching lines (most likely in front of the word Coincidencia).

This would also explain the message about the binary input.

At least two files in your directory have in their name a hexadecimal 0d character.

You should be able to verify this by doing a od -cx $dir1 instead of a cat $dir1.

CodePudding user response:

Please check your typo:

ls "$i" | grep -v -e '^total` -e *.$1 > $tempfile
                    ^^^    ^^^  ^^^

Were you searching for '^total' and *.$1?

ls "$i" | grep -v -e '^total' -e ".*\.$1" > $tempfile

Or were you searching fo '^total` -e *.$1'?

ls "$i" | grep -v -e "^total` -e *.$1" > $tempfile

Your quotes are not balanced and it's not clear if you are trying to use a position variable or trully dollar-one extension.

  • Related