我必须处理2775个文件夹,每个文件夹都包含一些文件。每个文件夹都命名为 conf+n,其中 n 是一个范围从 1 到 2775 的数字(例如 conf1、...conf250,...conf2775(。我创建了一些父文件夹,名为 A10、A20...A100、A200,...A1000,A2000等问题如下:我想移动前 100 个文件夹,因此 conf1-conf100 到 A100,conf101-con200 到 A200 等。
问题是文件夹总数 (2775( 不是固定的,因为我希望我的脚本适应不同范围的文件夹。
在我的试用脚本中,我创建了 250 个 conf# 文件夹以及 25 个 A# 文件夹,但是在将 conf1-conf10 移动到 A10、将 conf11-conf20 移动到 A20 等的过程中我遇到了困难。我试图为此创建一个迭代的 for 循环,但它失败了。
#!/bin/bash
for d in ./test_abc/
do (cd "$d" &&
for ((i=1;i<=250;i++))
do
mkdir $i
done);
done
while true; do
if [ "250" -le "500" ];
then
pfldrs=$((250 / 10))
echo -en 'n'
echo -e "x1B[31m CREATING BLOCKS ...x1B[0m x1B[32m $op x1B[0m"
echo -en 'n'
for d in ./test_abc/
do (cd "$d" &&
for ((k=1;k<=$pfldrs;k++))
do
m=$(($k*10))
mkdir A$m
done);
done
break
fi
done
我希望 A10 文件夹包含文件夹 conf1-con10,文件夹 A20 包含文件夹 conf11-conf20 等。
提前感谢您的帮助!
我只是做了一个简单的脚本,但我认为有更好的想法。
move.sh
#!/bin/bash
source_dir=$1
target_dir=$2
interval=$3
total_dir=$(basename $(find $source_dir -type d -name "conf*" | sort -Vr | head -1) | sed "s/conf//g")
echo "total $total_dir interval $interval"
target=0
for i in $(seq 1 $total_dir); do
if [ $i -gt $target ]; then
target=$(($target + $interval))
mkdir $target_dir/A$target
fi
mv $source_dir/conf$i $target_dir/A$target/
done
跑
./move.sh ${source_dir where conf* dirs are exist} ${target_dir where A* dirs will be created} ${interval}
ex) ./move.sh ./conf_dir ./move_dir 10