Quick tip: Create test data files containing patterns of bytes

Creating a file of “all zeroes” is pretty easy. You might start with dd if=/dev/zero ... or if you’re smarter, truncate --size=....

How about a file of all ones bytes, or alternating zeroes and ones?

A not very well known Perl operator called the “x” operator makes this quite easy:

$ perl -e 'print "\x01" x 100000' > output

makes a file 100000 bytes long containing all 1 bytes.

$ perl -e 'print "\x00\x01" x (100000/2)' > output

makes a file 100000 bytes long containing alternate 0 and 1 bytes.

$ perl -e 'print "\x00", "\x01" x (100000-2), "\x00"' > output

makes a file 100000 bytes long starting with a 0 byte, then 99998 1 bytes, then a final 0 byte.

You get the idea.

4 Comments

Filed under Uncategorized

4 responses to “Quick tip: Create test data files containing patterns of bytes

  1. Stephen Smoogen

    Thanks for this.. I run into this quite a bit..

  2. Karl

    quite easy to do in python too
    python -c “print ‘\x01’ * 10000” > output

  3. Shane Falco

    Very useful, as always with your blog.

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 )

Google+ photo

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

Connecting to %s