I want to modify a file so every byte from location 0x3000
to 0xDC000
is replaced with 0xFF
, everything else should be unmodified.
How to accomplish this with standard Linux tools?
This is jhnc's answer with little improvements (explained at the end of this answer). If jhnc decides to turn their comment into an answer please upvote their answer instead/too.
#! /bin/bash
overwrite() {
file="$1"; from="$2"; to="$3"; with="$4"
yes '' | tr \\n "\\$(printf %o "$with")" |
dd conv=notrunc bs=1 seek="$((from))" count="$((to-from))" of="$file"
}
In your case you would use the function from above like
overwrite yourFile 0x3000 0xDC000 0xFF
The start and end byte are both 0-based. The start is inclusive and the end is exclusive. Example:
$ printf 00000 > file
$ overwrite file 1 3 0x57
$ hexdump -C file
00000000 30 57 57 30 30 |0WW00|
00000005
Improvements made:
Fixed wrong
count=...
and explained interpretation of start and end.Allow filling with null bytes.
If you want to write null bytes0x00
you cannot useyes $'\x00'
. The null byte would represent the end ofyes
's argument string making the call equivalent toyes ''
. Sinceyes '' | tr -d \\n
produces no outputdd
will wait indefinitely.
The command presented in this answer allows you to fill the region with any byte (choose one from 0x00 to 0xFF).