Creating random files.

Here is a bash script to create 100 random files with random sizes smaller than 400KB. I needed this to quickly generate 100 files to use it on a stress test.

for ((i=1;i<101;i++)); do
size=`expr $RANDOM % 400`
dd if=/dev/urandom of=/tmp/testfile.$i bs=1024 count=$size
done

Explanation:

The for loop is pretty straightforward, it counts from 1 to 100.

size=`expr $RANDOM % 400`

This line generates a random number between 0 – 399

dd if=/dev/urandom of=/tmp/testfile.$i bs=1024 count=$size

This line generates a file with blocksize (bs / not bullsh!t) 1024 times the random number that we generated. The input for that file is the special urandom device in linux. (It’s the random number generator for the linux kernel)
The name of the file is also straightforward.

Leave a Reply