Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I create a new file and fill it with 1 Gigabyte worth of random data? I need this to test some software.

I would prefer to use /dev/random or /dev/urandom.

share|improve this question
add comment

2 Answers

up vote 18 down vote accepted

On most unices:

head -c 1M </dev/urandom >myfile

If your head doesn't understand the M suffix:

head -c 1048576 </dev/urandom >myfile

If your head doesn't understand the -c option (it's common but not POSIX; you probably have OpenBSD):

dd bs=1024 count=1024 </dev/urandom >myfile

Do not use /dev/random on Linux, use /dev/urandom.

share|improve this answer
    
For what it's worth, head doesn't understand -c on my Solaris 10 machine. –  rahmu Mar 7 '12 at 16:26
    
Interesting that head can read /dev/urandom, but tail cannot. –  Stefan Lasiewski Mar 12 '12 at 23:24
2  
@StefanLasiewski tail first tries to go to the end of the input file, which takes forever (literally). –  Gilles Mar 12 '12 at 23:25
    
Ah, like 'tail /dev/infinity`, if there was such a device. –  Stefan Lasiewski Mar 12 '12 at 23:42
1  
@StefanLasiewski You have /dev/zero as well, if you don't like variety. –  Gilles Mar 12 '12 at 23:55
add comment

Assuming that pseudo-random data is sufficient, dd if=/dev/urandom of=target-file bs=1M count=1000000 will do what you want.

dd(1) will read blocks of data from an input file and write them to an output file. The command line language is a little quirky, but it is one of those really useful tools worth mastering the basics of.

In this case if is input file, of is output file, bs is "block size" - and I used the GNU extension to set the size more conveniently. count is the number of blocks to read from if and write to of.

/dev/urandom is a better choice than /dev/random becuase, on Linux, it will fall back to strong pseudo-random data rather than blocking when genuinely random data is exhausted.

You may also want to look at http://www.random.org/ as another path to getting some random data without having to generate it yourself.

share|improve this answer
3  
A note -- unit specifications like 1M are not available in every standard dd variant. If your version of dd is affected, use bs=1048576. –  Chris Down Mar 7 '12 at 0:39
2  
The example command will create a file of count 1,000,000 blocks each of size 1 MB. That's about 1 TB (1M x 1MB), not 1 GB (which would be 1K x 1MB). As an aside, up to a point, increasing block size and reducing the block count tends to yield better throughput for a given amount of output data. –  Michael Kjörling Mar 7 '12 at 10:45
add comment

Your Answer

 
discard

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.