edit hex file in linux

How to Edit Hex Files in Linux

Binary files contain structured data stored in machine readable format. They are not human readable. Most software packages are available as binaries that you can download and use readily. Even when you install a package on your Linux system, the package manager ultimately installs a binary file for the program along with a few supporting files. Sometimes you may need to modify a binary file in Linux. One of the easiest ways to do this is to edit its hex equivalent. In this article, we will learn how to edit Hex files in Linux. Most Linux systems already contain many utilities to work with Hex files.


How to Edit Hex Files in Linux

Binary files are basically used to store all files except plain text files. They are used to store data, images, sound files, executables. By default, binary files cannot be viewed using regular text editors such as Vim or Nano. So we need to use special hex editors for this purpose.

Create Sample Hex File

Let us first create a sample hex file for our purpose. We will create a text file with some human readable content in it, and then convert it into a binary hex file. Open terminal and run the following command to create a text file.

$ echo "Hello world"  > sample.txt

Next we will convert sample.txt to sample.bin using hexdump command.

$ hexdump sample.txt > sample.bin

48656c6c6f20776f726c64

Once you have your hex file ready, you can use any of the hex editors to edit according your convenience. Xxd, ghex (GNOME hex editor), hexedit and jeex are some of the popular hex editors out there.

xxd is a popular utility that allows to create hex dump from binary data as well as convert hex data to binary. It comes pre-installed in most Linux distributions. It is a useful command that is a part of vim editor package. Please note, it can be used as a standalone tool as well as a builtin command to be used in Vim/Vi editor. Here is the command to open the above created sample.bin file using vim.

$ vim -b sample.bin

When you open the above file this way, you will be able to see its data in hex format. You can edit it as per your convenience.

Once you have made the required changes you can run the following command to convert it to text format.

:%!xxd -r

This will convert the hex data of your file into text format. Then we enter :wq to save the file.

In this article, we have learnt how to edit hex files in Linux. The key is to remember that we need to get the hex equivalent of a binary file, use a hex editor such as xxd to make changes to it, save the hex back to text format and finally save the file. If you are using other hex editors such as ghex you can directly open binary files in it. It will automatically open the file in hex format where you can make changes and directly save changes to the binary file.

Also read:

How to Add/Remove Software Repository in Fedora
How to Password Protect Vim File in Linux
How to Check Linux Server Location
How to Encrypt Drives Using LUKS in Linux
How to Limit Memory & Time of Processes in Linux

Leave a Reply

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