15

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?

3 Answers 3

30

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, this worked! I am still unsure what count means in this script? (I am new to scripting)
count is an argument to dd. man dd for more.
... specifically, count=1 will give you one block of output, and bs=1 will give you a block size of one - so a single byte each time, rather than 2 bytes in your example.
5

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).

1 Comment

The "-d" option is not available on macOS (10.13.6). Without it, the command example works, though then names such as "xaaaaa", "xaaaab" etc. are generated.
5

Not sure if you have any file naming requirements, but perhaps this:

for x in {1..20000}; do
  dd if=/dev/random of=test$x.dat bs=10000 count=4
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.