Linux command to retrieve a byte range from a file

I know that head and tail can take -c option to specify a byte offset. I'm looking for a way to efficiently extract a byte range from a large log file.


The DareDevil of the Unix commands, dd to the rescue!

dd if=yourfile ibs=1 skip=200 count=100

That would start from byte 200 and show 100 next bytes, or in other words, bytes 200-300. ibs means dd only reads one byte at a time instead of the default 512 bytes, but still writes out in default 512 byte chunks. Go and see if ibs harms the performance, I hope not.


If your interest is in the bytes, would od be of more interest.

-j, --skip-bytes=bytes
-N, --read-bytes=bytes

So to read the 16 bytes starting at byte 1024, and output in ascii

od -j 1024 -N 16 -a /bin/sh

You can use dd if=logfile of=pieceoflogfile skip=startingblock count=#ofblocks (possibly with bs=1 to get one-byte blocks, otherwise it uses 512 byte blocks). Not sure how efficient it is to tell it to write one byte at a time, though.