linux中递归复制⽂件夹,Shell脚本实现从⽂件夹中递归复制⽂
#!/bin/bash
linuxshell脚本怎么运行#desc: get file from directory
#example: sh getfilefromdir.sh a b
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