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 ?

  • 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 '14 at 0:18

dd if=/dev/random of=~/student/john.bin bs=1024 count=12000
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.

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


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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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