使用shell脚本生成随机文件

发布于 2024-08-28 01:40:30 字数 48 浏览 12 评论 0原文

如何在shell脚本中生成一个充满随机数字或字符的随机文件?我还想指定文件的大小。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千纸鹤带着心事 2024-09-04 01:40:30

使用dd命令从/dev/random读取数据。

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

这将读取 5000 个 1MB 的随机数据块,即总共 5GB 的随机数据!

尝试使用块大小参数以获得最佳性能。

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.

三月梨花 2024-09-04 01:40:30
head -c 10 /dev/random > rand.txt

将 10 更改为任意值。阅读“man random”了解 /dev/random 和 /dev/urandom 之间的差异。

或者,仅适用于 Base64 字符

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

Base64 可能包含一些您不感兴趣的字符,但没有时间想出更好的单行字符转换器...
(而且我们从 /dev/random 中获取了太多字节。抱歉,熵池!)

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!)

风和你 2024-09-04 01:40:30

一个好的开始是:

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

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
温暖的光 2024-09-04 01:40:30

创建 100 个随机命名的文件,每个文件大小 50MB:

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

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

嗼ふ静 2024-09-04 01:40:30

RANDOM 变量每次都会给你一个不同的数字:

echo $RANDOM

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

echo $RANDOM
转角预定愛 2024-09-04 01:40:30

另存为“script.sh”,以./script.sh SIZE 运行。 printf 代码取自 http://mywiki.wooledge.org/BashFAQ/071。当然,你可以用蛮力初始化 mychars 数组, mychars=("0" "1" ... "A" ... "Z" "a" ... "z"),但这不会有趣吗?

#!/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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文