Objective: Create a new file on Unix / Linux which is of certain size.
To generate a file with a given size, we can use the
dd utility. For example, to create a file called tmpfile that is 10MB in size, we can use the command below.
$ dd if=/dev/zero of=tmpfile bs=10M count=1 $ ls -l tmpfile -rw-r--r-- 1 root root 10485760 Jan 3 14:55 tmpfile
The above command will create an output file called tmpfile. The size of the output file is going to be the block size (specified by bs) multiplied by the count. For the example shown above, block size is set to 10MB and count is set to 1. We can achieve the same result by modifying the block size to 1MB and the count parameter to 10.
$ dd if=/dev/zero of=tmpfile bs=1M count=10 $ ls -l tmpfile -rw-r--r-- 1 root root 10485760 Jan 3 14:57 tmpfile
If you check the contents of the file, it will be filled with null (or zero) – this is generated from the
/dev/zero input file.
$ hexdump tmpfile 0000000 0000 0000 0000 0000 0000 0000 0000 0000 * 0a00000
If you need the output file to be filled with random content, change the input device to
/dev/urandom – depending on the type of device file that you have installed on your system.
$ dd if=/dev/urandom of=tmpfile bs=1M count=10 $ ls -l tmpfile -rw-r--r-- 1 root root 10485760 Jan 3 14:57 tmpfile
We can confirm that the contents contain random data by using
$ hexdump -n 16 tmpfile 0000000 fd07 d663 3fd6 9756 a826 fd30 b8b9 a2df 0000010