Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple files with random data with bash

How do I create multiple files (More than 20k, I need these files to run a test for syncying) with random data in OS X? I used a previously answered question (How Can I Create Multiple Files of Random Data?) that suggested to use something like

dd if=/dev/random bs=1 count=40000 | split -b 2

But using that gives me an error saying too many files.Any other way I can create a loop that will create files with any random data?

like image 695
bachkoi32 Avatar asked Jun 11 '13 00:06

bachkoi32


People also ask

How do you create multiple empty files in Linux?

Touch command to create multiple files: Touch command can be used to create the multiple numbers of files at the same time. These files would be empty while creation. Multiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here.

How do I create multiple files in Terminal?

To create multiple files just type all the file names with a single touch command followed by enter key. For example, if you would like to create 'myfile1' and 'myfile2' simultaneously, then your command will be: touch myfile1 myfile2.


2 Answers

You can do it with a shell for loop:

for i in {1..20000}; do dd if=/dev/urandom bs=1 count=1 of=file$i; done

Adjust count and bs as necessary to make files of the size you care about. Note that I changed to /dev/urandom to prevent blocking.

You can add some >/dev/null 2>&1 to quiet it down, too.

like image 114
Carl Norum Avatar answered Oct 04 '22 16:10

Carl Norum


The reason your approach doesn't work is that the default suffix for split (2 alphabetic characters) isn't a big enough namespace for twenty thousand files. If you add a couple of options:

dd if=/dev/random bs=1 count=40000 | split -b 2 -d -a 5

(where -d means "use digits, not alphabetic characters, for the suffix" and -a 5 means "use suffixes of length 5"), it ought to work fine - I tested it with /dev/urandom (for speed) on a GNU/Linux machine without problems.

Note that this is much faster than the for-loop approach of the other answers (2.5 seconds versus 43 seconds for Carl Norum's answer on my machine).

like image 35
Zero Piraeus Avatar answered Oct 04 '22 15:10

Zero Piraeus