Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7389268
  • 博文数量: 1755
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16227
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1755)

文章存档

2024年(1)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: LINUX

2009-03-27 10:30:59

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

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

阅读(2476) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-05-27 13:27:37

用rsync来同步文件夹是再好不过的了。 当然,如果是拿来练手,就当我没说。