olekb olekb - 4 months ago 11
Linux Question

How to update a byte in the middle of binary file in Linux shell?

When I run

on 2 files I get one byte difference:

cmp -l file1.dmp_byte file2.dmp
913462 0 100

How do I update byte 913462 of file file1.dmp with value 100?

Can it be done using standard Linux shell tools or Python?

Answer Source

In Python, you could use a memory-mapped file:

import mmap
with open('file1.dmp', 'r+b') as fd:
    mm = mmap.mmap(fd.fileno(), 0)
    mm[913462] = chr(100)