Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How To Create a File in Linux with Random Data  (Read 1712 times)

0 Members and 1 Guest are viewing this topic.

Vineesh K P

  • Guest
How To Create a File in Linux with Random Data
« on: February 17, 2018, 07:45:13 pm »
There are many cases where we want to create a file with a specific size, without worrying about the data it holds. In Unix systems, there are tools which can be used to accomplish this. Let us take a look at some of them and their usage.

head

You can specify the size using 'K' for Kilobytes, 'M' for Megabytes, 'G' for Gigabytes, etc. Take a look at the following example.

Code: [Select]
head -c 1G </dev/urandom >myfile
If your 'head' doesn't understand the 'G' suffix you can specify the size in bytes:

Code: [Select]
head -c 1073741824 </dev/urandom >myfile
dd

It will read blocks of data from an input file and write them to an output file. The command line language is a little quirky, but it is one of those really useful tools worth mastering the basics of.

Code: [Select]
dd if=/dev/urandom of=myfile bs=1M count=1000
In this case 'if' is input file, 'of' is output file, 'bs' is "block size" - and I used the GNU extension to set the size more
conveniently. (You can also use 1048576 if your dd doesn't have GNU extension.) 'count' is the number of blocks to read from
'if' and write to 'of'.

/dev/urandom is a better choice than /dev/random because, on Linux, it will fall back to strong pseudo-random data rather
than blocking when genuinely random data is exhausted.