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 but I am however 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.
- 5,924
- 9,380
7 Answers
if= is not required, you can pipe something into dd instead:
something... | dd of=sample.txt bs=1G count=1 iflag=fullblock
something... | head -c 1G > sample.txt
It wouldn't be useful here since openssl rand requires specifying the number of bytes anyway. So you don't actually need dd – this 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 iflag=fullblock
I would use bs=64M count=16 or similar, so that 'dd' won't try to use the entire 1 GB of RAM at once:
dd if=/dev/urandom of=sample.txt bs=64M count=16 iflag=fullblock
or even the simpler head tool – you don't really need dd here:
head -c 1G /dev/urandom > sample.txt
- 1,925
- 3
- 25
- 38
- 501,077
Create a 1GB.bin random content file:
dd if=/dev/urandom of=1GB.bin bs=64M count=16 iflag=fullblock
- 135
- 1,294
If you just need a somewhat random file which is not used for security related things, like benchmarking something, then the following will be significantly faster:
truncate --size 1G foo
shred --iterations 1 foo
It's also more convenient because you can simply specify the size directly.
- 208
- 2
- 7
Since, your goal is to create a 1GB file with random content, you could also use yes command instead of dd:
yes [text or string] | head -c [size of file] > [name of file]
Sample usage:
yes this is test file | head -c 100KB > test.file
- 161
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.
- 30,396
- 15
- 136
- 260
- 41
You could setup a bash-script for this like here. Get sure that you installed command 'pwgen' ... :
#!/bin/bash
touch cypher-01.txt
pwgen -sy 1024512 1024 >> cypher-01.txt
... this is finish and a file is created with randomly size of 1GB text. This takes on computer with 4 cores about ten minutes to fourteen minutes. To read this 1GB file with cat -A takes then about two and a half minutes.
- 133
Try this script.
#!/bin/bash
openssl rand -base64 1000 | dd of=sample.txt bs=1G count=1
This script might work as long as you don't mind using /dev/random.
#!/bin/bash
dd if=/dev/random of=sample.txt bs=1G count=1
- 179
- 418