Using Perl to make big files

A while ago, I talked about making big files. This was, by nature, UNIX-specific – that’s what I deal with all day, and the focus of this blog.

However, not all systems are UNIX (or Linux) – and not all the systems I deal with all day long are UNIX or Linux. However, perl is everywhere – and can be used quite easily to generate large files whatever you might be on.

For example, to make a 5M file, try this:

perl -e "open(FD, 'myfile'); print FD 'x' x (1024 * 1024 * 5);"

If you are inside of vim (a vi-clone which also runs everywhere), try this:

:!perl -e "print '-' x (1024 * 1024 * 5);"

This gives you a single line (5 megabytes in size). To make multiple lines:

:!perl -e 'for ($i = 1; $i < 500; $i++) { print "x" x 39, "\n"; };'

This makes 500 lines of 40 characters each (including single-character line terminator). If the system line terminator is two characters, then use 38 instead of 39. In total, this gives 19000 characters (about 18 kilobytes).

Perl is quite useful for creating portable scripts – but is by no means the only one. The ideas given here carry over to other languages that may be available. For instance, tcl and python and ruby are also available in other environments, and can do the same things as perl does here.

Of course, perl’s repetition operator ‘x’ makes it particularly easy here.

Update: corrected perl one-liner.

3 thoughts on “Using Perl to make big files”

  1. How does this works? I mean this:

    perl -e “open(F, ‘myfile’); print FD ‘x’ x (1024 * 1024 * 5);”

    I have executed that, and got nothing, tried with ‘touch myfile’ prior to executing it, execute the line above and didn’t got a 5 MB file.
    Also, tried the command above, and ‘> myfile’, but I still got the empty file I created with touch…
    With vi I don’t want to try, I’m a Emacs kinda person 😀

    Thanks.

  2. There’s a typo in that line – the F should be FD (or vice versa). See the (corrected) text.

    How it works is, it opens a file, then prints the letter ‘x’ to that file 1024 times 1024 times 5 times – which translates to 5Mb.

  3. I don’t know emacs well enough, but I’m sure there is a way to do the same in Emacs Lisp.

    Certainly, the Common Lisp formatting statements are beyond powerful enough to do the same. It takes a little getting used to (not every formatter uses C conventions!) but CL formatting is almost a language unto itself and about as powerful.

    I don’t see a quick way to do it in CL, but the ~{ ~} formatting construct probably has something to do with it. Check Common Lisp the Language (CLtL) for more information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: