Create a file of arbitrary size

Have you ever needed to create a file of a specific size? Not where the contents are anything specific, but you just need a file that is whatever size so you can test disk I/O or network transfer speed or whatever it is you want to test. For whatever reason, I have found it very handy to be able to create these test files when needed, so I thought I would pass along some tips to that end. Here are some ways you can accomplish this with Solaris and Linux.

In Solaris, this is pretty easy when you use the mkfile command, like this:

/usr/sbin/mkfile 10m testfile

This creates a 10 megabyte file called testfile, you can change the name and path to suit your needs. For each write() operation, there is no accompanying read() operation, making this method very efficient. Naturally you can specify different sizes like 10g for 10 gigabytes, etc. Very common sense like. Now, this doesn’t work on Linux, so for Linux and as an alternate method on Solaris or any other UNIX that doesn’t have mkfile, you can do the same thing with dd like this:

/usr/bin/dd < /dev/zero > testfile bs=1024 seek=10240 count=1

This creates a 10 megabyte file called testfile, you can change the name and path to suit your needs. This method does not require many reads and writes since the file is sparse. Please note that the path to dd differs from distro to distro (/bin on Linux, /usr/bin on Solaris, etc.). Like above, you can change the values specified on the command line to get files of differing sizes. For example, setting bs=2048 creates a 20 megabyte file.

Yet another way to create a file, is simply to cat dev zero, although you can’t really control the file size. It looks like this:

cat /dev/zero > testfile

This creates a file called testfile, you can change the name and path to suit your needs. This is the quickest and easiest way to just create some disk I/O, careful though because this will keep writing till the hard drive is full if you don’t break out of it. It’s good if you just want to hammer some disk spindles for some reason.

Hopefully this will help you out when you come across that time when you need some files for testing, or your testing the throughout speeds of your new storage array or something.

Tell me what you are thinking?