Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using the bash shell and would like to pipe the out of the command openssl rand -base64 1000 to the command dd such as dd if={output of openssl} of="sample.txt bs=1G count=1`. I think I can use variables however am unsure how best to do so. The reason I would like to create the file is because I would like a 1GB file with random text.

share|improve this question

2 Answers 2

up vote 30 down vote accepted

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.)

share|improve this answer
Thanks. A few questions, does using the command openssl rand -base64 $(( 2**30 * 3/4 )) > sample.txt give you a true text file? Secondly I don't quite follow the use of bs=64M count=16. Can you elaborate further? –  PeanutsMonkey Sep 6 '12 at 19:06
I posted a question regarding compressing large files at superuser.com/questions/467697/… and was advised that using /dev/urandom generates a binary file and not a true text file. –  PeanutsMonkey Sep 6 '12 at 19:10
@PeanutsMonkey: Right; you would need something like dd if=/dev/urandom bs=750M count=1 | uuencode my_sample > sample.txt. –  Scott Sep 6 '12 at 19:33
@PeanutsMonkey: There's no single "real world scenario", some scenarios might be dealing with gigabytes of text, others – with gigabytes of JPEGs, or gigabytes of compiled software... If you want a lot of text, download a Wikipedia dump for example. –  grawity Sep 6 '12 at 20:06
@PeanutsMonkey: The dd reads 750,000,000 bytes from /dev/urandom and pipes them into uuencode. uuencode encodes its input into a form of base64 encoding (not necessarily consistent with other programs). In other words, this converts binary data to text. I used 750M because I trusted grawity's statement that base64 encoding expands data by 33⅓%, so you need to ask for ¾ as much binary data as you want in your text file. –  Scott Sep 6 '12 at 20:07

Try this script.

$random=(openssl rand -base64 1000)
dd if=$random of="sample.txt bs=1G count=1"

This script might work as long as you don't mind using /dev/random.

dd if=/dev/random of="sample.txt bs=1G count=1"
share|improve this answer
I wouldn't recommend wasting /dev/random on this unless there's a very good reason to do so. /dev/urandom is much cheaper. –  Ansgar Wiechers Sep 6 '12 at 18:22
Also, $var=(command) isn't valid syntax in this context. –  grawity Sep 6 '12 at 18:58
@grawity - When you say it isn't valid, what do you mean? –  PeanutsMonkey Sep 6 '12 at 19:08
I mean exactly that – it's incorrect. –  grawity Sep 6 '12 at 19:22
@grawity, @PeanutsMonkey: He made a typo; he meant random=$(openssl rand -base64 1000). Although I would question whether bash would let you assign a gigabyte-long value to a variable. And even if you do say random=$(openssl rand -base64 1000), the subsequent if=$random doesn't make sense. –  Scott Sep 6 '12 at 19:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.