情景1:
现有一个目录dir_origin,该目录下有10000个子目录。现需要将该目录下的10000子目录,100个子目录为一批,分别移动到指定目录:1~100子目录移动到dir1,101~102子目录移动到dir2。只关注数目,不关注文件命名方式。
方法:
1. 用脚本创建100个格式化名称的dirNUM
#!/bin/bash
# make sure you route of cp_$1 is right
function getfiles(){
for ((i=1; i<=10; i ++))
do
`mkdir cp_$i`
done
}
getfiles $1
2.分批移动目录vim
#!/bin/bash
# make sure mv $1/$file cpdir/cp_$count_out have a right route
count_in=0
count_out=0
function getfiles(){
for file in `ls $1`
do
if [ $count_in -eq 10 ]
then
((count_out++))
count_in=0
fi
mv $1/$file cpdir/cp_$count_out
((count_in++))
done
}
getfiles $1
情景2 :
现有要求:
给定目录名称,建立给定名称(如AAA)的目录,并在该目录下建立 指定数目的具有一定命名规则(如cp_[NUM])的目录
从给定的目录(BBB)中获取1000个目录,每100个目录存储在AAA/cp_[num] 目录下:
创建目录:
#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
# ensure this .sh file is changed mod
# source is which dir you want to operate
function getfiles(){
# 建立指定目录
`mkdir cp_$1`
for ((i=1; i<=12; i ++))
do
#循环建立目录,i<=12,12指得是需要建立目录的数目
`mkdir cp_$1/cp_$i`
done
}
getfiles $1
移动数据:
#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
# ensure this .sh file is changed mod
# source is which dir you want to operate
#
count_in=0
count_out=1
function getfiles(){
for file in `ls $1`
do
# 每100个复制一次
if [ $count_in -eq 100 ]
then
# 复制100个之后,更换目录
((count_out++))
count_in=0
fi
mv $1/$file cpdir/cp_$count_out
((count_in++))
done
}
getfiles $1
情景3:
当前目录下目录命名错误,需要批量重命名的。文件在当前目录下运行: bash changename.sh ./ NEW_NAME
#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
# ensure this .sh file is changed mod
# source is which dir you want to operate
#
function getfiles(){
for file in `ls $1`
do
mv $file $2_$file
#echo $file
done
}
getfiles $1 $2