Skip to content

Efficiently Create Random Files for Testing and Development

Explore multiple techniques to generate random files efficiently for testing and development purposes. Utilize /dev/urandom to create files of specific sizes, generate files with random data using commands like 'head' and 'openssl', and produce files with random alphanumeric characters. Enhance your workflow with these versatile methods for generating random files.


Create Several Files Filled with Random Data

for fileSize in 1 10 50 100 250 500 1000; do 
    echo "Creating file: ${fileSize}M"; 
    sudo head -c ${fileSize}M </dev/urandom >${fileSize}; 
done

Create a File with Random Data Using 'head' Command

head -c 50M </dev/urandom >50MB
Create a File with Random Data Using OpenSSL
openssl rand -out sample.txt -base64 $(( 2**30 * 3/4 ))
openssl rand -base64 1000 | dd of=sample.txt bs=1G count=1

Create a File with Random Data Using 'head' Command

head -c 1073741824 </dev/urandom >myfile

Create a File with Random Data Using 'fallocate'

fallocate -l 100KB dummy_100KB_file

Generate a File with Random Alphanumeric Characters

< /dev/urandom tr -dc "\t\n [:alnum:]" | head -c1000 > file.txt