将所有文件从一个目录递归复制到另一个目录,但有例外


8

我如何将除了几个目录以外的所有文件从一个目录复制到另一个目录。例如:

cp -R test /www/test2 would copy all

但我想排除2个名为log和logs的文件夹。

我尝试了类似的东西:

find ~test -not -name logs,log |  cp -R test /www/test2 

但是您能帮我语法吗?

Answers:


10

使用rsync:

rsync -a --exclude=logs --exclude=log from/ to/

rynsc还挺酷的为我服务!
Micheal

rsync现在是我的新朋友:)谢谢,解释很简洁
Aleks 2014年

2

我认为您正在寻找类似的东西:

find dir -type d ! \( -name 'log' -o -name 'logs' \) | xargs -I{} cp -R {} /www/test2/{}

@sputnick看到我的更改。如果您碰巧知道它,可以随时提供emacs regex。
Tim Pote 2012年

0

如果使用

shopt -s extglob
cp -R test/!(log|logs) /www/test2

参见http://wiki.bash-hackers.org/syntax/pattern


结构如下所示会发生什么test/someotherdir/logs?OP从未说过log,它logs是第二高的目录。
Tim Pote 2012年

如果重新读取OP命令,则find ~test -not -name logs,log日志和日志应该是子目录。
吉尔斯·奎诺

find遍历所有子目录。文件没有。为了使您的答案有效,log并且logs必须是的直接子目录test
Tim Pote 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.