I have an img file which I had cloned from a disk of my RISC PC using dd:

dd if=/dev/hdb of=RISCPC.IMG

The size around 2GB.

I want to add 512 null bytes to the start and end of the file. What's the best way to do this using dd?

dd if=/dev/zero of=RISCPC.IMG.new bs=1 count=512
dd if=RISCPC.IMG of=RISCPC.IMG.new bs=512 seek=1
dd if=/dev/zero bs=1 count=512 >> RISCPC.IMG.new
mv RISCPC.IMG{.new,}

If you can use cat, you might consider this:

cat > RISCPC.IMG.new \
    <(dd if=/dev/zero bs=512 count=1) \
    <(dd if=/dev/zero bs=512 count=1)
mv RISCPC.IMG{.new,}
| improve this answer | |
  • dd: unrecognized operand ‘offset=512’ – frostschutz Jul 27 '13 at 9:27
  • @frostschutz Thanks, that's a typo. That should be seek. – Chris Down Jul 27 '13 at 11:20
  • 1
    Doing dd bs=1 on a whole disk image is going to take hours (1 read and 1 write system call per byte!). – Stéphane Chazelas Jul 27 '13 at 11:39
  • I would have done bs=512 count=1. – bahamat Jul 27 '13 at 17:11
  • @bahamat They should both be trivially fast, but true. – Chris Down Jul 27 '13 at 17:42

512 bytes at the end are added easily enough.

dd if=/dev/zero count=1 >> your.img

512 bytes at the start unfortunately require rewriting the entire image.

dd if=your.img of=your.img.new seek=1

It's very unusual to do this to a HDD image. So I'm wondering if this is what you really wanted.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.