Unix / Linux: Create File of Certain Size

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.

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.

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.

If you need the output file to be filled with random content, change the input device to /dev/random or /dev/urandom – depending on the type of device file that you have installed on your system.

We can confirm that the contents contain random data by using hexdump.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }