bashdd

How to make files with specific HEX pattern in Bash?


I'm trying to make many files (4KB~10MB) with specific 4-byte HEX pattern in Bash.

For example:

0000000: 1234 5678 1234 5678 1234 5678 1234 5678 ....

So I made zero pattern data using dd and converted it to specific 4-byte HEX pattern like below

dd if=/dev/zero bs=1k count=10 | tr "\000" $'\x12345678' > File.bin

But it returns only the first 1 byte of the given HEX pattern.

0000000 : 1212 1212 1212 1212 1212 1212 1212 1212 ....

Which point should I fix?


Solution

  • You could use perl

    $ dd if=/dev/zero bs=1k count=10 | perl -0777 -pe "s/\x00{4}/\x12\x34\x56\x78/g" > file.bin
    
    $ xxd file.bin 
    
    0000000: 1234 5678 1234 5678 1234 5678 1234 5678  .4Vx.4Vx.4Vx.4Vx
    0000010: 1234 5678 1234 5678 1234 5678 1234 5678  .4Vx.4Vx.4Vx.4Vx
    ...