Linux Commands

Grep –a Binary Files

On a Linux distribution, every document that doesn’t have text extension is referred to as a binary file, which includes everything from system commands and utilities to picture documents and generated applications. However, just because these documents are binary will not mean one can’t examine them.

In reality, you may use several instructions to retrieve information from binary format files or to show their contents. We’ll look at the option “-a” of grep instruction for Ubuntu 20.04 to process or execute binary file data as a text.

Let’s start by logging in from the Ubuntu 20.04 Linux system. Before going further, we will be updating our system to meet the requirements and avoid errors.

$ sudo apt-get update

Grep –a Binary Files

Make sure you have the latest utility of “grep” configured in your Ubuntu 20.04 system. It is necessary because we will be using the “grep” instruction for our binary file searching. The same “apt-get” package has been utilized here with the keyword “grep” to install it. The processing shows that it has been already configured at our end.

$ sudo apt-get install grep

Grep –a Binary Files

Binary File vs Text File

Within this illustration, we will take a look at the binary file and text file and have a broad look at their differences. We have already stated that the file is said to be binary if it doesn’t have a text extension or format. We have created 2 text-type files in the home directory using the “touch” instruction on our shell i.e. one.txt, and two.txt.

The file command, which recognizes documents by format, is among the simplest procedures for extracting information out of a binary format. The file instruction mostly overlooks the file extension which we use to appraise a document. Take note of how it replies to the following instruction applied to the text files i.e. results “empty”.

$ file one.txt

$ file two.txt

Grep –a Binary Files

Let’s say you have a jpeg format file named “baby” in your home directory i.e. image file. When you use the “file” instruction on it, it will display the output for this file in a variety of methods including analyzing the content, searching for a “magic number” (a file format indicator), and examining the syntax. As this file is an image, it shows its format and different standard measures.

$ file baby.jpeg

Grep –a Binary Files

Purpose of Grep -a

As per our research, it is mainly used to process any type of binary file as a simple text type file. Before using the “-a” option of grep instruction on different types of files, we will take a look at the “grep” manpage specially formulated for our help. The “man page” instruction is used for this purpose as shown below.

$ man grep

Grep –a Binary Files

The man page for “grep” is opened. You will see its name and syntax to use on the shell.

Grep –a Binary Files

Scroll down a little and within its “OPTIONS” area, you will find the “-a” option with its description and use for binary files. It states that it is used to process any binary documents as a simple text document and we can also use its alternative “—binary-files=text” on the shell.

Grep –a Binary Files

Grep –a On Bash File

Let’s create a new bash file to add some bash code to it with the “touch” instruction on the shell. The name of this file was assigned as “” and it got opened within Ubuntu’s “gnu nano” editor for ease and quick response.

Within this bash file, we added a bash support i.e. “#!/bin/bash”. After that, a single “echo” statement was used to print the text “Hello World” on the shell. Save this code for a while.

Grep –a Binary Files

While executing this bash file with the “bash” instruction on a shell, we have the “Hello World’ displayed on our Ubuntu’s shell screen.

$ bash

Grep –a Binary Files

Now, it’s time to utilize the grep “-a” instruction to execute the “” binary file. So, we used it with the “-a” option along with the pattern and the filename i.e. “”. As the double quotes were left with nothing on the first execution, it displayed the whole file code as text.

On using “space”, “/”, “#”, “echo”, and “Hello” patterns in other executions, the respective lines of the patterns were displayed while all the other lines were excluded.

$ grep –a “”

$ grep –a “ ”

$ grep –a “/”

$ grep –a “#”

$ grep –a “echo”

$ grep –a “Hello”

Grep –a Binary Files

You can also use the “grep –a” command combined with the “cat” command as shown below.

$ cat | grep –a “Hello”

$ cat | grep –a “World”

Grep –a Binary Files

Let’s use the alternative “—binary-files=text” of the “-a” option for the grep command on the ‘’ binary file. It shows the same output as we got for the “-a” option.

$ grep - -binary-files=text “”

$ grep - -binary-files=text “#”

$ grep - -binary-files=text “/”

$ grep - -binary-files=text “o”

$ grep - -binary-files=text “”

$ grep - -binary-files=text “echo”

Grep –a Binary Files

Grep –a On Image File

Let’s use the grep “-a” option for the “jpeg” binary file. So, we downloaded an image file “baby.jpeg” and placed it in the home folder.

$ ls

Grep –a Binary Files

It contains the shown-below baby image.

Grep –a Binary Files

On using the “grep –a” command on “baby.jpeg”, we got the ambiguous output unable to understand by a normal person. This is because the binary file contains pixel information that cannot be represented by simple text.

$ grep –a “” baby.jpeg

Grep –a Binary Files


This article demonstrates the use of the “grep” command along with its option “-a” to display the binary file data on the shell. We discussed the use of the “file” command to display the binary file information as compared to a simple text file. Lastly, we used the “grep –a” command on a bash file and an image file to display the content of these files as simple text output. After practicing these examples, you will be an expert on “grep” for Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *