Unix / Linux: Remove Bytes from Beginning of File

Objective: Remove first n bytes from a binary (or text) file on Unix / Linux.

Let’s say that we have a binary file called foo.bin and we need to strip the first 16 bytes from the file. To do that, we can either use the dd or tail utility. dd is the more safer when working on binary files.

To strip the first 16 bytes from the foo.bin file and write it to dd-out.bin file, use the following dd command syntax.

$ dd if=foo.bin of=dd-out.bin ibs=16 skip=1
1492153+1 records in
46629+1 records out
23874454 bytes (24 MB, 23 MiB) copied, 0.298316 s, 80.0 MB/s

The ibs parameter tells dd to read 16 bytes at a time. The skip parameter means: skip 1 ibs-sized (16 bytes) block at the start of input.

We can verify the header has been stripped by using hexdump on the input and output files.

$ hexdump foo.bin | head -n 4
0000000 0100 af01 168b 0a47 da6f 3402 236b 7bb3
0000010 4d2c 808a 45e9 b366 936f 9518 5631 d044
0000020 27a9 dbcc 9866 2f1e 3ad6 fd53 b9be 0e9a
0000030 0cf7 a24b 2183 4efb 0393 f5b6 461b e187

$ hexdump dd-out.bin | head -n 3
0000000 4d2c 808a 45e9 b366 936f 9518 5631 d044
0000010 27a9 dbcc 9866 2f1e 3ad6 fd53 b9be 0e9a
0000020 0cf7 a24b 2183 4efb 0393 f5b6 461b e187

As you can see, the first 16 bytes 0100 af01 168b 0a47 da6f 3402 236b 7bb3 have been removed from the output file.

To strip the header bytes using tail, use the following syntax.

$ tail -c +17 foo.bin > tail-out.bin

Note the -c +17 argument to tail. It means output starting with byte 17. The output is written to file tail-out.bin. We can verify the header content using hexdump.

$ hexdump tail-out.bin | head -n 3
0000000 4d2c 808a 45e9 b366 936f 9518 5631 d044
0000010 27a9 dbcc 9866 2f1e 3ad6 fd53 b9be 0e9a
0000020 0cf7 a24b 2183 4efb 0393 f5b6 461b e187

As you can see, both dd-out.bin and tail-out.bin files have the 16-byte header removed.

Ibrahim is a technology enthusiast with a keen interest in *NIX (Unix, Linux) systems, Android, open source and other tech related stuff. When his cpu load is low, you can probably find him online playing on his Xbox or PlayStation.