I was recently testing file upload performance, and needed several large files of different sizes to test with. To make the math easier, it was helpful if I had files with round numbered sizes like 10MB, 20MB, or 100MB. After searching around for files of the right size, it turns out the easiest solution is to generate one yourself using the Linux command line.
Depending on your needs, you can use two different methods of generating files using some simple commands.
Generating a File with Null Content
When you only care about the size of the file, and don’t care about the
file contents, you can use the /dev/zero
file. /dev/zero
is a special
Linux file made of null characters. When reading from /dev/zero
, the
reading application receives 0
repeatedly. We can use this fact along with
the dd
(disk duplicator) command to copy raw data from one source to
another.
For example, to create a 1MB file, run the following command:
dd if=/dev/zero of=output.dat count=1 bs=1M
The size of the file will be count * bs
megabytes. You can easily tweak
these values to create files of different sizes. For example, a 24MB file
can be created using:
dd if=/dev/zero of=output.dat bs=24M count=1
or
dd if=/dev/zero of=output.dat bs=1M count=24
Generating a File with Random Content
In cases where the content of the file is not important, but some lines
are desired, you can use the /dev/urandom
file can be utilized. Like
/dev/zero
, this Linux file provides a source of random bytes. By
combining it with the dd
command, we can generate a file with random
content like we did with the previous example.
For example, to create a 100MB file, run the following command:
dd if=/dev/urandom of=output.dat bs=1M count=100
While this approach provides some lines in the generated file, it should
be noted that the content will not be readable. Generating files using
/dev/urandom
is relatively slower compared to the /dev/zero
method.