Are you wondering how to combine binary files in Linux? This quick guide will discuss two approaches to how you can batch multiple binary files together in one go.
In Linux or any other operating system (Windows, Unix, Mac), there are files that contain machine code or compiled code. Such contents are not generally readable in a text editor. They are intended specifically for the programs which created them.
For example, a game can create a binary file to store the internal state of matches, and a program such as a word editor can store its files in binary format. Generally, binary files are in a format that is not easily recognizable.
How to Combine Binary Files on Linux
Sometimes, you can execute binary files, and they are called executable files. One can use different commands such as ‘
cp’, and ‘
cat’ on these binary files.
The ‘cat’ command is the simplest command that is used to show the file’s contents. To view the content of a file, use the following command:
In the command above, replace the ‘
file’ with the name of the file whose contents you want to display. The ‘
ccommand‘ by default, displays the file’s contents to the standard output stream. The ‘
cat’ command can also be used to append to the end of an existing file.
Combining the contents of binary files using the cat command
Now, we will discuss how we can merge the contents of various binary files. The cat command, which primarily works for text files, works for binary files as well. By using the shell functionality and the redirection operator ‘
>’, we can achieve the desired effect. Following are the basic steps of merging more than one binary file.
Make sure the files are present
First, make sure that files are present in the current directory. First, move to the desired directory by typing the following command:
Replace ‘/usr/d1’ with the directory where your binary files are present. And in order to ensure that all the binary files are present in the current directory, you can use the following command to display the directory’s contents:
After doing that, you will see the current directory contents. See if the files are present.
Copy the files to the desired directory
If the files are located in some other directory, you can copy them to the desired location by using the following command:
$cp /usr/d1/bin into /usr/d2/bin/
Here, you can provide the exact location of the source and destination (files and directories). Repeat the previous step for all the binaries. Now, you can check again if all binary files are located in your directory by following the first step.
Merge the contents of binary files
You can also use the cat command to output the contents of multiple files to a single combined file. For this purpose, you will use the redirection operator ‘
>’. The following command illustrates how we can combine the contents of multiple files:
$cat file1.bin file2.bin > file.bin
The previous command combines the contents of the files (‘
file1.bin’ and ‘
file2.bin’) and uses the redirection operator to combine them into ‘
file.bin‘. To copy more than two files, use the following command:
$cat file1.bin file2.bin file3.bin>file.bin
Specifying a large number of files
If there are multiple files in the current directory, you can specify all of them with the help of a wild card character ‘
*.bin’. Using this will select all the binary files present in the current directory. The following command illustrates how to specify all bin files of the current directory:
$cat*.bin > file.bin
Note that the order of files that are merged is not arbitrary.
Checking if the combined file has been created
It is important to remember that you will not see any output on the console. In order to know if the file has been created, type the following command on your console to list down the contents of the current directory:
If the file is already present, it will be overwritten. Otherwise, using this method will create a new file.
Appending to an existing file instead of overwriting
To append to an existing file instead of overwriting, you can use the double redirection operator ‘
>>’ as follows:
$cat file1.bin file2.bin file3.bin >> file.bin
Combining contents of the file using the dd command
The cat command is a bit slower and needs extra space. Thus we’re going to introduce you to an alternative called the dd command. This command can be also used to merge the file’s contents. The following command illustrates that:
In the above commands, ‘
file2.bin’, and ‘
file3.bin’ are combined to create file.bin.
Tip: You can use the ‘diff’ command with the ‘
-binary’ option to check for the differences in the contents of various binary files.
And that’s about it for this write-up, we thoroughly discussed how the cat command can be used in Linux to combine multiple binary files. Apart from understanding what a binary file is, we also talked about the various ways to combine the file and overwrite or append it to a file.
There are different graphical user interface options as well that can be used to merge binary files. And we focused on the basic approach to achieving that objective.
If this guide helped you, please share it. ?