dd
can write repeating \0
bytes to a file very fast, but it can't write repeating arbitrary strings.
Is there a bash-shell method to write repeating arbitrary strings equally as fast as 'dd' (including \0
)?
All the suggestions I've encountered in 6 months of Linux are things like printf "%${1}s" | sed -e "s/ /${2}/g"
, but this is painfully slow compared to dd
, as shown below, and sed
crashes after approximately 384 MB (on my box) -- actually that's not bad for a single line-length :) -- but it did crash!
I suppose that wouldn't be an issue for sed
, if the string contained a newline.
Speed comparison of dd
vs. printf
+sed
:
real user sys
WRITE 384 MB: 'dd' 0m03.833s 0m00.004s 0m00.548s
WRITE 384 MB: 'printf+sed' 1m39.551s 1m34.754s 0m02.968s
# the two commands used
dd if=/dev/zero bs=1024 count=$((1024*384))
printf "%$((1024*1024*384))s" |sed -e "s/ /x/g"
I have an idea how to do this in a bash-shell script, but there's no point re-inventing the wheel. :)
real/user/sys **0m4.565s**/0m0.804s/0m0.904s
..with a string "x\n", it tookr/u/s **0m30.227s**/0m29.202s/0m0.880s
... but that's still certainly faster thanprintf--sed
... The 384 byte string version is about the same speed asdd
on my system too...(it's funny how things vary... I actually got a slower dd speed this time... – Peter.O Apr 16 '11 at 19:48