您的位置:首页 > 运维架构 > Shell

shell脚本示例,复制名为dir1和dir2的目录下所有的文件到名为dir3的新目录

2018-02-26 14:43 706 查看
#! /bin/sh

if [ $# -eq 3 ]
then
dir1=$1
dir2=$2
dir3=$3

if [ -d $dir1 -a -d $dir2 ]
then
if [ -d $dir3 ]
then
echo "$dir3 already exits"
exit
else
cp -r -p $dir1 $dir3
echo -e "These files from $dir1 copied into $dir3: "
list=`ls $dir3`
echo $list

ls $dir3 | sort > a.txt
ls $dir2 | sort > b.txt

comm a.txt b.txt -1 -3 > new.txt
echo -e "\nThese new file(s) from $dir2 copied into dir3 "
cat new.txt| while read line
do
cp -p $dir2/$line $dir3
echo $line
done

comm a.txt b.txt -1 -2 > old.txt
echo -e "\nThese file(s) from $dir2 copied into $dir3 and overwrite(s) their namesakes in $dir3:"
cat old.txt| while read line
do
mTimeDir1=`find $dir2 -name $line -printf "%AY-%Am-%Ad %AH:%AM:%AS"`
mTimeDir2=`find $dir1 -name $line -printf "%AY-%Am-%Ad %AH:%AM:%AS"`
t1=`date -d "$mTimeDir1" +%s`
t2=`date -d "$mTimeDir2" +%s`
if [ $t1 -lt $t2 ]; then
cp -p $dir2/$line $dir3
echo $line
fi
done
fi
else
echo "$dir1 or $dir2 not exits"
exit
fi
else
echo "Less Parameters"
fi

rm a.txt b.txt new.txt old.txt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: