Skip to content
Related Articles
Open in App
Not now

Related Articles

How to suppress binary file matching results in grep

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 02 Jan, 2023
Improve Article
Save Article
Like Article

In this article, we will discuss the topic of grep. Also, we will discuss what are the problems that occurred during binary files while using the grep command. Lastly, we will discuss how we can solve this problem using grep options. 


The grep, Global Regular Expression Print is a command line utility that is used to print the string after matches. grep is one of the most useful commands in Unix/Linux systems. grep works as it copies a line into the buffer and then it compares with the search string. grep is made up of three commands egrep, grep, and fgrep that works in a similar way. 

Problems with Binary Files using grep

Step 1: Binary Encoding

The problems occur when we try to print some specific word from a string but rather than printing that word it prints the binary file name. For example, we created a file with a UTF encoding error “\x80” and it displays binary file matches.

we have created a file encoding.txt using the echo command

echo 'Encoding\x80' >> encoding.txt


Print the specific word using grep

grep "Encoding" encoding.txt


Step 2: NULL Byte

A null byte is a byte that contains a value 0 which means 0x00 hex. when the grep command will be used then grep will treat as a binary file.

We have created a text file named null.txt that has a hex value of 0x00

echo “This file contains NULL\0x00” >> null.txt


When we try to grep that null file, it will show binary file matches

grep "NULL" null.txt


Grep with Binary Files

Step 3: Using the grep command without suppressing Binary Files

 In Linux/Unix, there is a directory present called “/bin” that contains the user’s binary file. so we will use the grep command without any options used.

grep "B" /bin/zstd


Grep without Binary Files

Step 4: Use the grep command with option -I for suppressing Binary Files

The grep command has a lot of options available for doing alternative tasks, to suppress the binary file, we have an option as -I or –binary-files=without-match

grep -I /bin/zstd


grep –binary-files=without-match “B” /bin/zstd


Step 5: Using the grep command with option -n 

We can print the number of lines for searching that specific string. we can use option -n with the grep command.

cat /etc/smi.conf | grep -n "load"


Step 6: Use the grep command with option -H

When we have to print the file name along with the output then, grep has an option -H that can make use of it.

grep -H "path" /etc/smi.conf



In the above article, we have covered the definition of grep, the problems occurred when we use any binary file and finally, we have seen how we can take the option of grep (-I) for suppressing binary file matches using grep.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!