可以把1亿个txt文本随机分配成n个文件夹,一个文件夹15个txt. 可以做到吗

可以把1亿个txt文本随机分配成n个文件夹,一个文件夹15个txt.

Math.random();

#!/bin/bash

fileno=1000 #FIXME: change its value to your real file number
subsize=15
subdno=$((fileno/subsize))
if [ $((fileno%subsize)) -ne 0 ]; then
subdno=$((subdno+1))
fi

for i in seq ${fileno}; do #FIXME: replace this loop with your file list
while [ 1 ]; do
dindex=$((RANDOM%subdno))
if [ ! ${dfilled[${dindex}]} ]; then
mkdir subd${dindex}
dfilled[${dindex}]=0
fi
if [ ${dfilled[${dindex}]} -lt ${subsize} ]; then
break
fi
done

#FIXME: move your i-th file to a subdirectory
touch subd${dindex}/${i}.txt

dfilled[${dindex}]=$((dfilled[${dindex}]+1))

done

之前的格式变乱了,重发一下:

#!/bin/bash

fileno=1000 #FIXME: change its value to your real file number
subsize=15
subdno=$((fileno/subsize))
if [ $((fileno%subsize)) -ne 0 ]; then
    subdno=$((subdno+1))
fi

for i in `seq ${fileno}`; do #FIXME: replace this loop with your file list
    while [ 1 ]; do
        dindex=$((RANDOM%subdno))
        if [ ! ${dfilled[${dindex}]} ]; then
            mkdir subd${dindex}
            dfilled[${dindex}]=0
        fi
        if [ ${dfilled[${dindex}]} -lt ${subsize} ]; then
            break
        fi
    done

    #FIXME: move your i-th file to a subdirectory
    touch subd${dindex}/${i}.txt

    dfilled[${dindex}]=$((dfilled[${dindex}]+1))
done 

大约需要15TG左右的空间