3

Can anyone suggest a way to write a script that creates a text file filled with random data of exactly 12 MB ? Say I'm interested in creating within the script the file /home/student/john.bin that consists solely of random data and its size would be exactly 12MB ?

1
  • I should have asked this before I answered, but do you mean random 1's and 0's, or random ASCII characters or something to that effect?
    – MGodby
    Dec 13, 2014 at 0:18

2 Answers 2

5

dd if=/dev/random of=~/student/john.bin bs=1024 count=12000
or
dd if=/dev/urandom of~/student/john.bin bs=1024 count=12000

Where bs is the size of each random input you want (1024 = 1Kb), and count is how many of those blocks you want.

ie.
bs=1024, count=12000: 12MB
bs=512, count=24000: 12MB
bs=256, count=48000: 12MB
etc.

0
3

dd out of /dev/urandom should get the job done. If you need more "purity" to the randomness, you can also use /dev/random.

i.e. :

mgodby@mgodby-nb1:~$ dd if=/dev/urandom of=/home/mgodby/blah.crap bs=4 count=3000000
3000000+0 records in
3000000+0 records out
12000000 bytes (12 MB) copied, 7.73399 s, 1.6 MB/s
mgodby@mgodby-nb1:~$ 

Tune your "block size" and "count" to get the exact amount of data that you want.

Note: Using /dev/random may instead may take significantly longer than /dev/urandom , but it will be "more random." See the following article for more information:
Wikipedia - /dev/random

2
  • (+1) Thank you for sharing useful information.
    – user43389
    Dec 13, 2014 at 0:40
  • Urandom is way faster than random on my system
    – ioan
    Dec 18, 2018 at 23:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .