Sometimes – rarely – you need to remove a few bytes from one end of a file. ere’s a quick way of doing it with the UNIX tool dd, so you don’t have to write your own program for it.
I started this post because I spent a lot of time looking for ways to do this. There were tons of ways of cutting bytes off the end of a file, but hardly any resources on slicing them off the beginning. In the end, I settled for the following solution:
dd if=$FILE_TO_TRIM skip=$BYTES_TO_SKIP bs=1 | dd of=$OUTPUT_FILE
The first command splits the file into blocks of 1 byte and skips the first specified number of bytes. This is then piped to dd again, which writes the result to an output file. Normally, using a block size of 1 byte means that you write very slowly, but since the block size is only used for the pipe, you end up writing the file in your file system’s native block size.
Cutting bytes off the end of a file is even easier – there’s even a tool for it. It works like this:
truncate -s $DESIRED_FILE_SIZE $INPUT_FILE
This either truncates the file down to the specified size or extends it using zero bytes. Be aware that this works in place, so you might want to do something like this:
cp $INPUT_FILE $OUTPUT_FILE truncate -s $DESIRED_FILE_SIZE $OUTPUT_FILE