Friday, May 20, 2011

Remove First N bytes From a (Binary) File

Situation:
You have a (binary) file, and you need to remove the first n bytes of data in it.

Solution:
Use dd. Read more about it, here.

Here is how to do it:
$ dd if=input_file ibs=1 skip=N of=output_file

N is the number of bytes to be removed.

example:
$ dd if=input_file.dat ibs=1 skip=3 obs=10M of=output_file.dat


Now, what if you need to remove the first N bytes and the last M bytes from a file?
$ dd if=input_file.dat ibs=1 skip=N count=X of=output_file.dat

To calculate X:
X = acutal_file_size_in_bytes - N - M


I recommend you to read through the man pages, and play around with ibs, obs, bs, count values and other parameters that might be useful.

6 comments:

  1. Cool beans! Thanks. A minor improvement. You can remove N bytes faster by switching the block size and skip size.

    dd if=input_file ibs=N skip=1 of=output_file

    Vinai
    --
    www.ece.purdue.edu/~vsundar

    ReplyDelete
  2. OH COME ON, CAN SOMEONE FROM THE FUCKING WORLD TELL HOW TO FUCKING DO THIS FROM WINDOWS WITH A FUCKING BAT FILE?!?!

    ReplyDelete
    Replies
    1. Install a copy of cygwin. And you can then have access to a large cross section of the standard unix tool set in Windows.

      Delete
  3. Yeap, remove Windows and switch to Linux ;)

    ReplyDelete
  4. Sometimes I need to fück my win7

    ReplyDelete