The following lines of code in a text file will do approximately what (I think) you are asking for: copy a file from one location to another, but replace a block of bytes at one location with a block from another location; it uses dd
as requested. However, it does create a separate output file - this is necessary to ensure there is no conflict regardless of whether the "input" block occurs before, or after, the "replacement" block. Note that it will do nothing if the distance between A and B is less than the size of the block being replaced - this would result in overlap, and it's not clear if you would want the bytes in the overlapping area to be "the end of A" or "the start of the copy of A".
Save this in a file called blockcpy.sh
, and change permissions to include execute
(e.g. chmod 755 blockcpy.sh
). Run it with
./blockcpy.sh inputFile outputFile from to length
Note that the "from" and "to" offsets have base zero: so if you want to copy bytes starting at the start of the file, the from
argument is 0
Here is the file content:
#!/bin/bash
# blockcpy file1 file2 from to length
# copy contents of file1 to file2
# replacing a block of bytes at "to" with block at "from"
# length of replaced block is "length"
blockdif=$(($3 - $4))
absdif=${blockdif#-}
#echo 'block dif: ' $blockdif '; abs dif: ' $absdif
if [ $absdif -ge $5 ]
then
# copy bytes up to "to":
dd if=$1 of=$2 bs=$4 count=1 status=noxfer 2>0
# copy "length" bytes from "from":
dd bs=1 if=$1 skip=$3 count=$5 status=noxfer 2>0 >> $2
# copy the rest of the file:
rest=$((`cat $1 | wc -c` - $4 - $5))
skip=$(($4 + $5))
dd bs=1 if=$1 skip=$skip count=$rest status=noxfer 2>0 >> $2
echo 'file "'$2'" created successfully!'
else
echo 'blocks A and B overlap!'
fi
The 2>0
" 'nix magic" suppresses output from stderr which otherwise shows up in the output (of the type: "16+0 records in
").