📅 July 29, 2013
Have you ever needed to create a file of a specified size containing random data? One simple way is to use the dd command. The following command creates a 1 GB file of junk, random data.
time dd if=/dev/urandom of=data.bin ***=1073741824 count=1
The file size is exactly 1,073,741,824 bytes, which is 2^30. Even though the file properties might read 1.1 GB, the size in bytes will be exactly one gigabyte.
How Does It Work?
- dd is the command.
- if specifies the source of data.
- /dev/urandom is the built-in random number generator from which dd gets its bytes.
- of specifies the output file, which is named data.bin in this example.
- *** is the block size. This works with count to get the final size of the file.
- count tells how many blocks to create.
- time records how long it takes to create the 1 GB file. This part is not necessary and has nothing to do with the dd command. Using a solid-state drive (SSD), it takes about 1 minute 20 seconds to create a 1 GB file.
The block size ( ***) and count (count) options are multiplied together to get the final size. This example uses a block size of 1073741824 bytes and writes one block of that. The result will be a 1 GB file.
We did not have to do it this way. We could also specify a block size of 1048576 bytes and a count of 1024 to produce a 1 GB file. (1,048,576 x 1,204 = 1,073,741,824)
time dd if=/dev/urandom of=data.bin ***=1048576 count=1024
There are plenty of options available and many ways to use dd (such as grabbing the master boot record (MBR) from a hard drive), so consult man dd for more ideas.
The convenient part about dd is that it is included with Linux by default (at least in every distribution I have tried), so there is no need to install any additional software.
Simply open a terminal and have fun!