将文件夹2中有,文件夹1没有的文件拷贝到文件夹1中

sh/bash/dash/ksh/zsh等Shell脚本
回复
slimhigh
帖子: 67
注册时间: 2010-05-06 19:29

将文件夹2中有,文件夹1没有的文件拷贝到文件夹1中

#1

帖子 slimhigh » 2011-12-02 21:58

编写一个脚本,把文件夹2里面有,而文件夹1里面没有的文件,复制到文件夹1里面。

代码: 全选

#!/bin/sh
bar1=$1
bar2=$2
for file in `ls $bar2` ; do
	flag=`find $bar1 -name $file -print`  
	if [ -z "$flag" ] ; then 
		cp $bar2/$file $bar1
		echo "copy $bar2/$file to $bar1/$file"
	fi 
done
大家有更简洁的方式吗?
头像
kingkongmok
帖子: 340
注册时间: 2006-03-23 14:48

Re: 将文件夹2中有,文件夹1没有的文件拷贝到文件夹1中

#2

帖子 kingkongmok » 2011-12-02 23:38

代码: 全选

cp -avrn 2/* 1/
ps,上面的好像不能拷贝.foobar,所以还是rsync比较好。

代码: 全选

rsync -av --ignore-existing 2/ 1
slimhigh
帖子: 67
注册时间: 2010-05-06 19:29

Re: 将文件夹2中有,文件夹1没有的文件拷贝到文件夹1中

#3

帖子 slimhigh » 2011-12-04 19:48

kingkongmok 写了:

代码: 全选

cp -avrn 2/* 1/
ps,上面的好像不能拷贝.foobar,所以还是rsync比较好。

代码: 全选

rsync -av --ignore-existing 2/ 1
好奇怪的命令啊。。你是怎么想到的。。
回复