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.
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.
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 two.txt
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.
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.
The man page for “grep” is opened. You will see its name and syntax to use on the shell.
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 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 “new.sh” 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.
While executing this bash file with the “bash” instruction on a shell, we have the “Hello World’ displayed on our Ubuntu’s shell screen.
Now, it’s time to utilize the grep “-a” instruction to execute the “new.sh” binary file. So, we used it with the “-a” option along with the pattern and the filename i.e. “new.sh”. 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 “ ” new.sh
$ grep –a “/” new.sh
$ grep –a “#” new.sh
$ grep –a “echo” new.sh
$ grep –a “Hello” new.sh
You can also use the “grep –a” command combined with the “cat” command as shown below.
$ cat new.sh | grep –a “World”
Let’s use the alternative “—binary-files=text” of the “-a” option for the grep command on the ‘new.sh’ binary file. It shows the same output as we got for the “-a” option.
$ grep - -binary-files=text “#” new.sh
$ grep - -binary-files=text “/” new.sh
$ grep - -binary-files=text “o” new.sh
$ grep - -binary-files=text “” new.sh
$ grep - -binary-files=text “echo” new.sh
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.
It contains the shown-below baby image.
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.
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.