How can I populate a file with random data?

On most unices:

head -c 1G </dev/urandom >myfile

If your head doesn't understand the G suffix you can specify the size in bytes:

head -c 1073741824 </dev/urandom >myfile

If your head doesn't understand the -c option (it's common but not POSIX; you probably have OpenBSD):

dd bs=1024 count=1048576 </dev/urandom >myfile

Do not use /dev/random on Linux, use /dev/urandom.


Assuming that pseudo-random data is sufficient, dd if=/dev/urandom of=target-file bs=1M count=1000 will do what you want.

dd(1) will read blocks of data from an input file and write them to an output file. The command line language is a little quirky, but it is one of those really useful tools worth mastering the basics of.

In this case if is input file, of is output file, bs is "block size" - and I used the GNU extension to set the size more conveniently. (You can also use 1048576 if your dd doesn't have GNU extension.) count is the number of blocks to read from if and write to of.

/dev/urandom is a better choice than /dev/random becuase, on Linux, it will fall back to strong pseudo-random data rather than blocking when genuinely random data is exhausted.

You may also want to look at http://www.random.org/ as another path to getting some random data without having to generate it yourself.


while true;do head /dev/urandom | tr -dc A-Za-z0-9;done | head -c 5000K | tee  5000kb

Used this to generate 5MB random character data. If you need different size, change the -c value of head, change the outfile name, execute and wait until the execution completes.

Tags:

Random