专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

实现一个文件夹同步的shell脚本

百度收藏 QQ搜藏
作者: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
上一篇:安全删除和恢复文件的shell脚本 下一篇:slackware版本添加用户adduser脚本欣赏

power by soyo123 2007-2008