How can I create a binary file using Bash?
Asked Answered
B

5

19

How can I create a binary file with consequent binary values in Bash?

Like:

hexdump testfile

0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000010 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e
0000020 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e
0000030 ....

In C, I do:

fd = open("testfile", O_RDWR | O_CREAT);
for (i=0; i< CONTENT_SIZE; i++)
{
    testBufOut[i] = i;
}

num_bytes_written = write(fd, testBufOut, CONTENT_SIZE);
close (fd);

This is what I wanted:

#! /bin/bash
i=0
while [ $i -lt 256 ]; do
    h=$(printf "%.2X\n" $i)
    echo "$h"| xxd -r -p
    i=$((i-1))
done
Brakeman answered 15/12, 2011 at 13:58 Comment(1)
Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar insteadMagalimagallanes
A
22

There's only one byte you cannot pass as an argument in a Bash command line: 0

For any other value, you can just redirect it. It's safe.

echo -n $'\x01' > binary.dat
echo -n $'\x02' >> binary.dat
...

For the value 0, there's another way to output it to a file

dd if=/dev/zero of=binary.dat bs=1c count=1

To append it to file, use

dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1
Ajar answered 12/1, 2012 at 7:35 Comment(1)
Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.Handstand
C
11

Take a look at xxd:

xxd: creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form.

Coltun answered 15/12, 2011 at 14:2 Comment(1)
For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.Bland
U
2

If you don't mind to not use an existing command and want to describe you data in a text file, you can use binmake. That is a C++ program that you can compile and use like following:

First get and compile binmake (the binary will be in bin/):

git clone https://github.com/dadadel/binmake
cd binmake
make

Create your text file file.txt:

big-endian
00010203
04050607
# Separated bytes not concerned by endianness
08 09 0a 0b 0c 0d 0e 0f

Generate your binary file file.bin:

./binmake file.txt file.bin
hexdump file.bin

0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000008

Note: you can also use it with standard input and standard output.

Unthread answered 17/6, 2016 at 13:35 Comment(1)
If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).Sampling
M
1

Use the below command,

i=0; while [ $i -lt 256 ]; do echo -en '\x'$(printf "%0x" $i)''  >> binary.dat; i=$((i+1));  done
Marseillaise answered 15/11, 2018 at 21:42 Comment(0)
P
0

In Unix, and Unix-like systems, the developers forgot about this ability built into previous and competing systems (by way of a monitor program in ROM).

There is no way in this family of systems to create a binary file from keyboard input, without first transferring a program to do so.

It is because at the time (mid 1970s), this path diverged from the competing paths, user-accessible (interpreter), and user-usable (built in).

Thus, a compiler is a dependency necessary to create a binary on these systems, even though very few bytes of code is required in ROM to allow a user to enter a binary file, including an executable one, on any computer.

Paulie answered 25/10, 2023 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.