Insights on Ruby, Git, jQuery, Cappuccino, WordPress, Debian and OS X. Please subscribe if you find something useful!

Create Arbitrarily Sized Files In Less Than 15 Lines of Ruby

Posted: May 22nd, 2009 | Author: Jerod | Filed under: Ruby | Tags: , | Comments

Ok, so I’m having fun with this “in less than 15 lines of Ruby” idea, but this time I turn my attention away from Twitter (I know, that’s hard to do right now) and toward file generation.

Sometimes you just need a 1GB file. Or a 350MB file. Or a 1MB file. It doesn’t matter what is in that file, but size does matter (heh).

With this little Ruby script, you can arbitrarily generate files of any size (using 1MB increments).

The Script:

The resulting file will be named “[X]MB” where X is the size you requested.

A Note:

This script is derived from one I found awhile ago somewhere on the internet (don’t remember where). It didn’t work correctly (file sizes were off) and was much more verbose.

Enjoy!


  • joemi
    Or, if you've got a unix-like terminal available, you could just use "dd if=/dev/zero of=FILENAME bs=1m count=1" on the command line and change the "1" in "count=1" to however many megs you want. Also, you can change "if=/dev/zero" to "if=/dev/urandom" to make the file be filled with random data instead of zeroes.

    Granted, the Ruby method has some error control and automatic file naming, and it fills the file with a user-defined string, instead of just zeroes or just random numbers.
  • You absolutely right, and dd is a powerful tool. The advantage of using this script instead is not having to remember which flags and settings to use to accomplish the task (plus the ones you listed).

    Of course, you could always set up a shell function to wrap the dd command if you wanted.
blog comments powered by Disqus