博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

剑心通明的资料库

文章均为转载,本人不负因参考它所导致的一切后果,请谨慎参考!如您的文章不愿被转载,请点击此处联系本人!
  jxtm.cublog.cn

关于作者
姓名:剑心通明
职业:高级工程师(专修灵魂^_^)
年龄:20出头30不到
位置:网络上一节点
个性介绍:努力学习每一天!
倾心打造:http://www.bsdlover.cn
http://bbs.bsdlover.cn
BSD爱好者的乐园!
|| << >> ||
我的分类


实现一个文件夹同步的shell脚本
作者:devel
算法:
1.先find /pathname -print >filea #获取两个文件里所有文件的全名。
2,比较文件,先删去要同步的文件夹里多余的文件。接着重新获得需要同步的文件夹里所有文件的全名。
3.比较文件,把源文件夹里增加的文件CP到要同步的文件夹中。
这个脚本是同步/share目录里的所有文件,需要备份到/mnt/d/share
代码:
#!/bin/bash
mount /dev/hda6 /mnt/d 2>/dev/null ; unalias rm cp
rm /share/c/app/*o /share/c/tmp/*o /share/c/app/*core /share/c/tmp/*core /share/c/app/a.out /share/c/tmp/a.out
find /share -print >/tmp/.share_    #把/share的所有文件的全名保存到/tmp/.share_
find /mnt/d/share -print |sed 's/\/mnt\/d//g' >/tmp/.d_
chmod 700 /tmp/.share_ /tmp/.d_
count=0
for i in $(comm -23 /tmp/.d_ /tmp/.share_) ; do     #比较两个文件里/tmp.d_ 与/tmp./share_的不同。
echo "/mnt/d$i"
rm "/mnt/d$i" ; count=$((count+1))    #删除/mnt/d/share/*里的多余的文件和计数。
done    #for command ;do command ; done的循环到此结束。
echo "del $count file at /mnt/d/share/"
find /mnt/d/share -print |sed 's/\/mnt\/d//g' >/tmp/.d_ ; count=0    #重新获得文件的全名。初始化计数器。
for i in $(comm -23 /tmp/.share_ /tmp/.d_) ; do
echo $i
cp $i "/mnt/d$i" ; count=$((count+1))    #备份/share里新增加的文件到/mnt/d/share,同时计数。
done
echo "already copied $count file from /share to /mnt/d/share " ;sync
umount /mnt/d 2>/dev/null
echo done

 原文地址 http://www.bsdlover.cn/html/18/n-818.html
发表于: 2008-05-03,修改于: 2008-05-03 10:50,已浏览391次,有评论0条 推荐 投诉


网友评论
 发表评论