Edit binary files with dd: replacing, deleting, and copying bytes
01 January, 2026
Time to read: 1 mins
Last update: 01 January, 2026
Table of contents
Replace bytes with dd (hex editor, edit bytes)
# The file you want to edit:
$ echo 'My name is dark' > file.txt
$ cat file.txt
My name is dark
# Replace 1 byte:
$ echo -n 'M' |
dd \
of=file.txt \
bs=1 \
seek=11 \
count=1 \
conv=notrunc
1+0 records in
1+0 records out
1 byte copied, 5.1489e-05 s, 19.4 kB/s
# Inpect file:
$ cat file.txt
My name is Mark
$ xxd file.txt
00000000: 4d79 206e 616d 6520 6973 204d 6172 6b0a My name is Mark.Notes:
- stdin = input file = ‘if’, in other words, what you want to add to the new file
- of is the file you want to edit.
- bs, read and write up to BYTES bytes at a time (default: 512); overrides ibs and obs
- seek, The decimal address you want to start replacing. If you want to add a hex address, wrap in double quotes and use arithmetic substitution
$((0x30)). - count, how many bytes from the ‘if’ you want to add to the ‘of’.
- conv=notrunc, do not truncate the file, which is the default.
Delete bytes with dd
It seems that you can’t remove individual bytes. If you think about it, there isn’t a system call that does this.
What you can do though is to write the whole file again, without those bytes:
$ echo 'helllo doge world ' |
{ dd bs=1 count=4 ; dd skip=1 bs=1 count=2 ; dd skip=5 bs=1 ; } 2>/dev/nullExplanation:
- print hell
- skip 3rd l, print ‘o’ and space.
- delete doge and space and print world.
See https://unix.stackexchange.com/questions/128871/dd-remove-range-of-bytes
Copy a part of a file with dd
$ dd if=input.txt iflag=skip_bytes,count_bytes skip=5 count=3 of=part.txt
# or
$ dd if=input.txt bs=1 skip=5 count=3 of=part.txtThere are 2 versions because if you use the skip/count combo without a block size (bs) skip=N and count=N, the N is a block and not bytes. So, if you want to skip bytes, and you don’t want to change the block size, use the 1st option (set the block size to 1 byte). Otherwise, you can use the second option.
Other things to read
Popular
- Reveal animations on scroll with react-spring
- Gatsby background image example
- Extremely fast loading with Gatsby and self-hosted fonts