Create a random file of a specific size

dd if=/dev/zero of=testfile.txt bs=1M count=10
This will create a 10 MB file named testfile.txt. Change the count parameter to change the size of the file. As one commenter pointed out, yes /dev/random can be used, but the content doesn't matter if you just need a file of a specific size for testing purposes, which is why I used /dev/zero. The file size is what matters, not the content. It's 10 MB either way. "Random" just referred to "any file - content not specific"
Sample Output
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.038701 s, 271 MB/s

By: mstoecker
2009-06-17 17:06:16

What Others Think

This doesn't create a random file, it creates a zero'd file. What you mean is: dd if=/dev/random of=testfile.txt bs=1M count=10
dfego · 643 weeks and 6 days ago
I'm with dfego. With the modification, though, this is a great oneliner.
hooobs · 643 weeks and 6 days ago
Might want to consider '/dev/urandom'. Wikipedia says: A counterpart to /dev/random is /dev/urandom ("unlocked" random source) which reuses the internal pool to produce more pseudo-random bits. This means that the call will not block, but the output may contain less entropy than the corresponding read from /dev/random. The intent is to serve as a cryptographically secure pseudorandom number generator. This may be used for less secure applications.
mungewell · 643 weeks and 6 days ago
Mod'ed up as this is actually really usefull (with '/dev/zero') for creating a blank block onto which to re-image embedded files systems. The problem is that all the old deleted files are really still there. If you 'dd' a blank page and then 'tar'/'untar' the filesystem onto it you can get your compression ratio back up.
mungewell · 643 weeks and 6 days ago
@mungewell I had also considered using /dev/urandom as the input source as it will also work and I have used it before. Like I said, content doesn't really matter if all you need is the size, which is what I needed for download/upload speed testing, and how I came up with this. @dfego I don't know if it works for you, but using /dev/random never tends to generate more than 1 kb of data in the file for me. As mungewell pointed out, I believe this is do to the fact that /dev/random does not reuse bits of noise from the entropy pool it uses to create the random data and thus the reason why I didn't use it in the initial code.
mstoecker · 643 weeks and 6 days ago
"Random" means "random". Unfortunately, too many people have recently begun using it to mean things like "anonymous", "unknown", "miscellaneous", etc. @mstoecker: Sometimes the content *does* matter.
dennisw · 643 weeks ago
Create an alias for bash function mkfile { eval dd if=/dev/urandom of=$2 bs=1M count=$1 } mkfile
nublaii · 572 weeks ago
I don't wanna live forever great assistance
yeoyeo · 9 weeks and 2 days ago
I don't wanna live forever great assistance
yeoyeo · 9 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: