Occasionally, the need arises for files of a specific size. Transferring said files between hosts, for example, can provide a quick indication of your network throughput. One easy way to build a file of a specific size is with the Data Description or dd command. The dd command is one of the original Unix utilities, used to perform low-level copying of a specified input file to the specified output file (standard input to standard output is the default) according to operands, while optionally performing conversions on the raw data. You’ll often see it used to create an image of a entire disk or the disk’s Master Boot Record, or to make a disk from an image.
Let’s open a terminal in Linux and create a file named “test-file” that’s one kilobyte (decimal units) in size:
1 2 |
mkdir ~/testfiles && cd ~/testfiles dd if=/dev/zero of=test-file bs=1KB count=1 |
You should see something that resembles the following output:
1 2 3 |
1+0 records in 1+0 records out 1000 bytes (1.0 kB) copied, .000145457 s, 6.9 MB/s |
To create a larger file, say one megabyte or one gigabyte, replace the KB multiplicative suffix in the bs operand with MB or GB respectively:
1 |
dd if=/dev/zero of=test-file bs=1GB count=1 |
How about a file that’s 1.5 gigabytes? You can accomplish this by adjusting the bs multiplicative suffix and the number of blocks in the count operand:
1 |
dd if=/dev/zero of=test-file bs=1MB count=1500 |
To use binary units (multiplication by a power of 2) instead of decimal units, simply drop the “B” in the bs multiplicative suffix. Let’s recreate our test file using binary units (one megabyte = 1048576 bytes):
1 2 3 4 |
dd if=/dev/zero of=test-file bs=1M count=1 1+0 records in 1+0 records out 1048576 bytes (1.0 MB) copied, 0.002878 s, 364 MB/s |
Note for users of FreeBSD (and possibly other Unix-like operating systems), the dd command supports binary units only. For example, attempting to use bs=1MB instead of bs=1M will result in an error.
There you have it. A nice simple way to create files of a specific size for network testing or whatever your needs might be. Leave comment if you have a favorite use for the dd command.
Excellent Article. Keep up the good work
Thanks for the kind words preejith.