generate a random file using shell script
Asked Answered
T

6

9

How can i generate a random file filled with random number or character in shell script? I also want to specify size of the file.

Tout answered 6/4, 2010 at 17:53 Comment(1)
Which characters are allowed in the output file? any random byte or just ascii alphanumeric bytes?Furie
S
21

Use dd command to read data from /dev/random.

dd if=/dev/random of=random.dat bs=1000000 count=5000

That would read 5000 1MB blocks of random data, that is a whole 5 gigabytes of random data!

Experiment with blocksize argument to get the optimal performance.

Spondaic answered 6/4, 2010 at 17:55 Comment(4)
dd -bs=1024 count=1 if/dev/random of=test it doesn generate the right file size i want...Tout
@Tadeusz: One should usually use bs=1M if he wants MiB, not MB.Howlet
After a second read of the question, i think he also wanted to save only characters (guessing alphabetic ones) and numbers to the file.Furie
That dd command is unlikely to complete as there will not be 5 gigabytes of entropy available. Use /dev/urandom if you need this much "randomness".Anthraquinone
F
8
head -c 10 /dev/random > rand.txt

change 10 to whatever. Read "man random" for differences between /dev/random and /dev/urandom.

Or, for only base64 characters

head -c 10 /dev/random | base64 | head -c 10 > rand.txt

The base64 might include some characters you're not interested in, but didn't have time to come up with a better single-liner character converter... (also we're taking too many bytes from /dev/random. sorry, entropy pool!)

Furie answered 6/4, 2010 at 17:59 Comment(2)
oops, missed the characters and numbers part, i'm guessing you mean alphanumeric characters... need to revise.Furie
Saving entropy: the latest trend in egologists' green living. :)Oliverolivera
A
3

A good start would be:

http://linuxgazette.net/153/pfeiffer.html

#!/bin/bash
# Created by Ben Okopnik on Wed Jul 16 18:04:33 EDT 2008

########    User settings     ############
MAXDIRS=5
MAXDEPTH=2
MAXFILES=10
MAXSIZE=1000
######## End of user settings ############

# How deep in the file system are we now?
TOP=`pwd|tr -cd '/'|wc -c`

populate() {
    cd $1
    curdir=$PWD

    files=$(($RANDOM*$MAXFILES/32767))
    for n in `seq $files`
    do
        f=`mktemp XXXXXX`
        size=$(($RANDOM*$MAXSIZE/32767))
        head -c $size /dev/urandom > $f
    done

    depth=`pwd|tr -cd '/'|wc -c`
    if [ $(($depth-$TOP)) -ge $MAXDEPTH ]
    then
        return
    fi

    unset dirlist
    dirs=$(($RANDOM*$MAXDIRS/32767))
    for n in `seq $dirs`
    do
        d=`mktemp -d XXXXXX`
        dirlist="$dirlist${dirlist:+ }$PWD/$d"
    done

    for dir in $dirlist
    do
        populate "$dir"
    done
}

populate $PWD
Attitude answered 6/4, 2010 at 18:0 Comment(0)
S
1

Create 100 randomly named files of 50MB in size each:

for i in `seq 1 100`; do echo $i; dd if=/dev/urandom bs=1024 count=50000 > `echo $RANDOM`; done

Stercoricolous answered 11/10, 2012 at 2:28 Comment(1)
It's better to use mktemp to create random files. for i in seq 1 100; do myfile=mktemp --tmpdir=. dd if=/dev/urandom bs=1024 count=50000 > $myfile doneHirai
B
0

The RANDOM variable will give you a different number each time:

echo $RANDOM
Binomial answered 6/4, 2010 at 17:58 Comment(0)
S
0

Save as "script.sh", run as ./script.sh SIZE. The printf code was lifted from http://mywiki.wooledge.org/BashFAQ/071. Of course, you could initialize the mychars array with brute force, mychars=("0" "1" ... "A" ... "Z" "a" ... "z"), but that wouldn't be any fun, would it?

#!/bin/bash
declare -a mychars
for (( I=0; I<62; I++ )); do
    if [ $I -lt 10 ]; then
        mychars[I]=$I
    elif [ $I -lt 36 ]; then
        D=$((I+55))
        mychars[I]=$(printf \\$(($D/64*100+$D%64/8*10+$D%8)))
    else
        D=$((I+61))
        mychars[I]=$(printf \\$(($D/64*100+$D%64/8*10+$D%8)))
    fi
done
for (( I=$1; I>0; I-- )); do
     echo -n ${mychars[$((RANDOM%62))]}
done
echo
Sanctuary answered 28/7, 2011 at 1:18 Comment(1)
The /dev/random & base64 approach is also good, instead of piping through base64, pipe through "tr -d -c [:alnum:]", then you just need to count the good chars that come out until you're done.Sanctuary

© 2022 - 2025 — McMap. All rights reserved.