1. Overview

We sometimes need to view and edit binary files. Most Linux distributions have built-in utilities to manipulate binary files. We can even perform conversions from binary to text format and vice versa.

In this tutorial, we’ll learn what binary files are and look at utilities to perform read/write operations on them.

2. What Are Binary Files?

Binary files contain data that is structured and stored in computer-readable format. It is made up of binary numbers or bits that can easily be interpreted by our computer.

Binary files make up every other thing stored in our computer except plain text files. They are commonly used for data files, images, sound files, executable programs, etc.

An advantage of programs stored in binary is that they can be executed very quickly because they are smaller than text files with an equivalent amount of data.

By default, we cannot view or edit binary files with normal programs or text editors. Instead, we need special utilities and hex editors to access binary files.

Let’s explore some of these utilities and see how we can use them to edit binary files.

3. Setup

Let’s create a text file named sample.txt that we’ll convert to a binary file using hexdump:

$ echo "BAELDUNGISAWESOME baeldungisawesome"  > sample.txt

$ hexdump sample.txt > sample.bin

0000000 4241 454c 4455 4e47 4953 4157 4553 4f4d
0000010 4520 6261 656c 6475 6e67 6973 6177 6573
0000020 6f6d 650a 
0000024

We’ll be using this sample.bin file throughout this tutorial to perform edits with different tools.

4. Editing with xxd

The xxd command allows us to dump hexadecimal data from a binary file easily. We can also convert hexadecimal data back into a binary file. It’s a useful command-line hex editor that’s usually part of the vim text editor package.

xxd command comes built-in in almost all the major Linux distributions.

Using our sample.bin from above, let’s type in the following command to open the file through vim as a binary file:

$ vim -b sample.bin

Since our file is in hexadecimal format, we can enter editing mode on vim and change 42 41  to 62 61. The hex digits 42 41 represent the letters “B and A” and 62 61 represent the letters “b and a”.

Let’s also change 62 61 on the second line to 42 41.

Once we’ve made the changes, we can type in the following command on vim to convert our file to text format:

:%!xxd -r
baELDUNGISAWESOME BAeldungisawesome

Once we convert it to text format, we can notice that the capital letters “BA” at the beginning of our string is swapped with the small letters “ba” at the beginning of the second part of our string.

We can save the changes by typing in the following command on vim’s terminal:

:wq 

5. Editing with ghex

GNOME Hex Editor is a simple binary editor that we can use to load raw data from binary files and display them for editing. It allows us to view and edit binary files in both hex and ASCII formats.

The display is split into two columns, with hexadecimal values in one of the columns and ASCII representation in the other.

We can download and install GNOME Hex Editor using our local package manager:

$ sudo apt install ghex

Once installed, let’s type in the following command to open our sample.bin file:

$ ghex sample.bin 

ghex

We can see a lot of helpful information about our file. We can also edit the hexadecimal values of our string.

An advantage of GHex is that we can see in real-time how our changes are affecting the string in our sample.bin file.

Once we’re done editing, we can hit Ctrl+S to save our changes.

6. Editing with hexedit

hexedit is a command-line tool that we can use to view and edit binary files. Hexedit presents the data in ASCII and hexadecimal formats.

It also supports the searching of data by values and can show scrollable output. We also have access to a wide range of keyboard shortcuts to navigate through the hex data.

hexedit is readily available in all major Linux distributions. If required, we can install it using the package manager:

$ sudo apt install hexedit

Once installed, let’s type in the following command to open our sample.bin file:

$ hexedit sample.bin

hexedit

We can change the hex values of our file and see in real-time how they affect the string on our file.

To save our changes, we can type Ctrl+S.

7. Editing with jeex

jeex is a simple hexadecimal editor that allows us to create, open, and edit files in binary, hexadecimal, ASCII, and octal. It also includes features like delete, insert, copy and paste, search, and many more.

Furthermore, jeex shows us a lot of information about the file we’ve opened, like modification timestamp, last access timestamp, mode bits, and more.

jeex is readily available in all major Linux distributions. If required, we can install it using the package manager:

$ sudo apt install jeex

Once installed, let’s type the following command to open our sample.bin file:

$ jeex sample.bin

jeex

We can see a lot of useful information about our file. Moreover, we can edit the hexadecimal values of our file and see how they affect the ASCII values of our string.

Typing Ctrl+S saves the changes.

8. Editing with hexcurse

hexcurse is a ncurses-based hex editor utility. We can use it to open, edit, and save binary files. It also provides us with a lot of useful editing and searching features.

We can also easily navigate to a specific line because of hexcurse’s friendly terminal interface.

hexcurse is readily available in all major Linux distributions. If needed, we can install it using the package manager:

$ sudo apt install hexcurse

Once installed, we can open our sample.bin file by typing the following command:

$ hexcurse sample.bin

hexcurse

We can use our keyboard to navigate through the file and edit the hexadecimal values. We can also see how our changes affect the text in our file in real-time.

Once done, we can click on the Save button at the bottom.

9. Conclusion

In this article, we learned what binary files are and touched upon a few popular binary file editors. We also discussed how to use them to make changes to our binary files.