I'd like to generate dummy files in bash. The content doesn't matter, if it was random it would be nice, but all the same byte is also acceptable.
My first attempt was the following command:
rm dummy.zip; touch dummy.zip; x=0; while [ $x -lt 100000 ]; do echo a >> dummy.zip; x=`expr $x + 1`; done;
The problem was its poor performance. I'm using GitBash on Windows, so it might be much faster under Linux but the script is obviously not optimal.
Could you suggest me a quicker and nice way to generate dummy (binary) files of given size?
Method # 1 – Use the mktemp or tempfile utility to create temporary random file name. The script or command should quit if it didn't get a safe temporary file using the exit command.
You can try head
command:
$ head -c 100000 /dev/urandom >dummy
You may use dd for this purpose:
dd if=/dev/urandom bs=1024 count=5 of=dummy
Note, that
x=`expr $x + 1`;
isn't the most efficient way to calculation in bash. Do arithmetic integer calculation in double round parenthesis:
x=((x+1))
But for an incremented counter in a loop, there was the for-loop invented:
x=0; while [ $x -lt 100000 ]; do echo a >> dummy.zip; x=`expr $x + 1`; done;
in contrast to:
for ((x=0; x<100000; ++x)) do echo a done >> dummy.zip
Here are 3 things to note:
But there is still a more simple form of the for-loop:
for x in {0..100000} do echo a done >> dummy.zip
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With