How can I copy arbitrary part of a binary file with reasonable speed?
Apparently, dd
with bs=1
is very slow, while setting bs
to another value makes it impossible to copy arbitrary parts.
Is it wrong? Is it possible to do with dd
? If not, then what is the tool?
For example, this command,
dd if="$img" of=tail.bin bs=2147483648 skip=1 status=progress
copies an incorrect tail.
And this command,
dd if="$img" of=tail.bin bs=1 skip=2147483648 status=progress
is very slow.
strace dd if=file of=/tmp/tail.bin bs=2147483648 skip=1
appears to do the right thing, firstlseek(0, 2147483648, SEEK_CUR)
on the input, thenread(0, "\205..."..., 2147483648) = 2147479552
andwrite(1, buf, read_size)
, then anotherread(0, buf, 2147483648)
gets to the end of the file, returning much shorter instead of slightly shorter. After printing a partial-read warning, it writes out that partial block, then read(0, buf, bs) returns 0. So it ends and prints"0+2 records in\n0+2 records out\n"
. – Peter Cordes Mar 29 '23 at 03:03skip_bytes
is of course even better. Or for the whole tail,tail -c +offset
. But if you want to write to the middle of a file withconv=notrunc
, you wantdd
. – Peter Cordes Mar 29 '23 at 03:06cmp --ignore-initial=2147483648:0 src tail.bin
confirmstail.bin
is identical. Oh, but apparently ifdd
is interrupted during aread
system call, it can lose data? – Peter Cordes Mar 29 '23 at 04:24count=N
, since it limits the total number of input blocks it reads, counting partial ones; or 2) you useconv=sync
, when it fills the partial blocks with NULs; or 3) the device you write is blocksize sensitive (where I supposesync
could help in some cases). But having to think any of this should work as evidence thatdd
usually isn't the right tool for generic byte-stream copying :) – ilkkachu Mar 29 '23 at 08:05bs
was so large I got a short read. – Peter Cordes Mar 29 '23 at 15:11Range
header? ;-) – Boldewyn Mar 30 '23 at 08:04