I am looking for a command to create multiple (thousands of) files containing at least 1KB of random data.

For example,

Name            size
file1.01        2K
file2.02        3K
file3.03        5K

How can I create many files like this?

  • What did you mean by “exclusive”? It doesn't make sense in context, so you probably used the wrong word. – Gilles 'SO- stop being evil' May 1 '15 at 21:54
  • I meant that files can not have the same content. – João José May 2 '15 at 10:59
  • So, exclusive would have meant unique. – somehume Feb 28 '19 at 22:09

Since you don't have any other requirements, something like this should work:

#! /bin/bash
for n in {1..1000}; do
    dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 ))

(this needs bash at least for {1..1000}).

  • 2
    This needs bash for numerous reasons, including $((…)) and $RANDOM.  Even $(…) might not exist in every shell. – G-Man Says 'Reinstate Monica' May 7 '15 at 21:30
  • 1
    @G-Man, in any case, none of those features are specific to bash nor did they originate in bash ({1..1000} comes from zsh, for n in...; done and variable expansion comes from the Bourne shell, $(...), $((...)) and $RANDOM come ksh). The features that are not POSIX are {1..1000}, $RANDOM and /dev/urandom. – Stéphane Chazelas Mar 3 '17 at 13:46
  • 1
    If you wanted 1..1000 to be constant-width you need "%04d" in which case bash or zsh can do {0001..1000} with no printf – dave_thompson_085 Mar 3 '17 at 16:32
  • Additional feature: I need a way to spread these files over lots of randomly named nested subdirectories. – stu Feb 14 '18 at 18:09

A variation with seq, xargs, dd and shuf:

seq -w 1 10 | xargs -n1 -I% sh -c 'dd if=/dev/urandom of=file.% bs=$(shuf -i1-10 -n1) count=1024'

Explanation as requested per comments:

seq -w 1 10 prints a sequence of numbers from 01 to 10

xargs -n1 -I% executes the command sh -c 'dd ... % ...' for each sequence number replacing the % with it

dd if=/dev/urandom of=file.% bs=$(shuf ...) count=1024 creates the files feeded from /dev/urandom with 1024 blocks with a blocksize of

shuf -i1-10 -n1 a random value from 1 to 10

  • Wat does this exactly do? – saruftw Mar 3 '17 at 13:00
  • 1
    @saru95 explanation added. – FloHimself Mar 3 '17 at 13:35
  • This creates 10 files with random size. Change "10" to the number of files wanted. – Nay Jan 15 '19 at 22:38

You can do something like this:

while [ $filecount -lt 10000 ] ; do
    base64 /dev/urandom | 
    head -c "$filesize" > /tmp/file${filecount}.$RANDOM
  • I tried doing this but not worked. You might want to explain the parameters. :) – João José May 2 '15 at 10:54
  • very fast thx @rahul – BG Bruno Apr 21 '19 at 9:07

This uses a single pipeline and seems fairly fast, but has the limitation that all of the files are the same size

dd if=/dev/urandom bs=1024 count=10240 | split -a 4 -b 1k - file.

Explanation: Use dd to create 10240*1024 bytes of data; split that into 10240 separate files of 1k each (names will run from 'file.aaaa' through 'file.zzzz')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.