How do I create a 1GB random file in Linux?

if= is not required, you can pipe something into dd instead:

something... | dd of=sample.txt bs=1G count=1

It wouldn't be useful here since openssl rand requires specifying the number of bytes anyway. So you don't actually need ddthis would work:

openssl rand -out sample.txt -base64 $(( 2**30 * 3/4 ))

1 gigabyte is usually 230 bytes (though you can use 10**9 for 109 bytes instead). The * 3/4 part accounts for Base64 overhead, making the encoded output 1 GB.

Alternatively, you could use /dev/urandom, but it would be a little slower than OpenSSL:

dd if=/dev/urandom of=sample.txt bs=1G count=1

Personally, I would use bs=64M count=16 or similar:

dd if=/dev/urandom of=sample.txt bs=64M count=16

Create a 1GB.bin random content file:

 dd if=/dev/urandom of=1GB.bin bs=64M count=16 iflag=fullblock

If you want EXACTLY 1GB, then you can use the following:

openssl rand -out $testfile -base64 792917038; truncate -s-1 $testfile

The openssl command makes a file exactly 1 byte too big. The truncate command trims that byte off.

Tags:

Linux

Dd