Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Bash] files with random content

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
linuxCowboy
Posts: 15
Joined: 2022-09-22 09:02
Been thanked: 1 time

[Bash] files with random content

#1 Post by linuxCowboy »

I need some test files of different size and random content.

Solutions with dd or head are not straight enough.

The work should be done in the script. Using it should be a no-brainer.

I ended up with this:

Code: Select all

Frandfile ()
{
        [[ ! $1 || $1 =~ "-h" || ! $1 =~ ^[0-9]+[kmgKMG]?$ ]] &&
                echo "
        $FUNCNAME size [file{fr-(size)}] # create file with size randoms

        # k 1000  K 1024  m 1000000 M 1048576  g 1000000000 G 1073741824
" && return

        # the inevitable separator
        local CHR='.'
        local SEP='perl -pe'\''while(/\d{5,}/){$b=$`;$m=$&;$a=$'\'"\'"\'';
		       $m=~s/(?<=\d)(?=(\d{3})+\b)/'$CHR'/;$_="$b$m$a"}'\'

        local SIZE=$1
        local FILE=${2-fr-$SIZE}

        SIZE=`echo $SIZE |sed 's/k/000/;
			       s/m/000000/;    
			       s/g/000000000/'`

        SIZE=`echo $SIZE |perl -pe 's/(\d+)K/$1 * 1024/e;
				    s/(\d+)M/$1 * 1048576/e;
				    s/(\d+)G/$1 * 1073741824/e'`

        echo -n "create $FILE with `echo $SIZE |eval "$SEP"` bytes ... "

        local TIME=`date +%s`

        head --bytes $SIZE /dev/urandom >$FILE || return

        TIME=$((`date +%s` - $TIME))

        printf "%d second%s\n\n" $TIME `(($TIME == 1)) || echo "s"`

        case $FILE in
                */*)    ls -l $FILE
                        ;;
                fr-*)   ls -ltr fr-*
                        ;;
                *)      ls -l $FILE
                        ;;
        esac
        echo
}
Only kilo, mega and giga. (nix kibi or kiwi... ;-)

lower is even, upper is power and a default file name.

That's it. Plain and simple.

originaly I used

Code: Select all

openssl rand -out $FILE $SIZE
but they limit themselves to RAND_MAX (2GB)!

Example:

Code: Select all

 $ Frandfile 100M
create fr-100M with 104.857.600 bytes ... 2 seconds

-rw-r--r-- 1 lxc lxc     1,263,616 03.10. 2023 21:06:53 fr-1234K
-rw-r--r-- 1 lxc lxc     1,234,000 03.10. 2023 21:07:03 fr-1234k
-rw-r--r-- 1 lxc lxc     1,234,567 03.10. 2023 21:07:09 fr-1234567
-rw-r--r-- 1 lxc lxc 1,234,567,000 03.10. 2023 21:07:38 fr-1234567k
-rw-r--r-- 1 lxc lxc   104,857,600 03.10. 2023 21:25:22 fr-100M

linuxCowboy
Posts: 15
Joined: 2022-09-22 09:02
Been thanked: 1 time

Re: [Bash] files with random content

#2 Post by linuxCowboy »

Sometimes I need Zeros:

        $FUNCNAME size [z] [file{f[rz]-(size)}]  # create file with size randoms [zeros]

Code: Select all

Frandfile ()
{
        [[ ! $1 || $1 =~ "-h" || ! $1 =~ ^[0-9]+[kmgKMG]?$ ]] &&
                echo "
        $FUNCNAME size [z] [file{f[rz]-(size)}]  # create file with size randoms [zeros]

        # k 1000  K 1024  m 1000000 M 1048576  g 1000000000 G 1073741824
" && return

        local CHR='.'
        local SEP='perl -pe'\''while(/\d{5,}/){$b=$`;$m=$&;$a=$'\'"\'"\'';
                        $m=~s/(?<=\d)(?=(\d{3})+\b)/'$CHR'/;$_="$b$m$a"}'\'

        local SIZE=$1

        local FILE="fr"
        local CONT="urandom"
        [[ $2 = z ]] && FILE="fz" && CONT="zero" && shift

        FILE=${2-$FILE-$SIZE}

        SIZE=`echo $SIZE |sed 's/k/000/;
                               s/m/000000/;
                               s/g/000000000/'`

        SIZE=`echo $SIZE |perl -pe 's/(\d+)K/$1 * 1024/e;
                                    s/(\d+)M/$1 * 1048576/e;
                                    s/(\d+)G/$1 * 1073741824/e'`

        echo -n "create $FILE with `echo $SIZE |eval "$SEP"` $CONT bytes ... "

        local TIME=`date +%s`

        head --bytes $SIZE /dev/$CONT >$FILE || return

        TIME=$((`date +%s` - $TIME))

        printf "%d second%s\n\n" $TIME `(($TIME == 1)) || echo "s"`

        case $FILE in
                */*)
                        ls -l $FILE
                        ;;
                f[rz]-*)
                        ls -ltr f[rz]-*
                        ;;
                *)
                        ls -l $FILE
                        ;;
        esac
        echo
}

Post Reply