需求
前两天碰到需要在十层左右的文件夹中提取文件的需求,于是写了此脚本。
如下面这样的文件结构:
脚本
脚本getfilefromdir.sh如下:
init_path=${1%/}
save_path=${2%/}
function checksavepath() {
if [ -d $save_path ]
then
rm -rf $save_path
fi
mkdir ${save_path}
touch $save_path".log"
}
function getfilefromdir(){
for file in ` ls $1`
do
if [ -d $1"/"$file ]
then
getfilefromdir $1"/"$file
else
local path="$1/$file"
local name=$file
if [ ! -f $save_path"/"$name ]
then
echo "cp ${path} to ${save_path}/${name}"
cp ${path} "${save_path}/${name}"
else
echo "${path} file already exists"
echo "${path}" >> $save_path".log" 2>&1
fi
fi
done
}
checksavepath
for sfol in ${init_path}
do
getfilefromdir ${sfol}
done
运行
如果有同名文件,会存在dir2.log中
结果为:
如对本文有疑问, 点击进行留言回复!!
网友评论