cp命令用于排除某些文件被复制

有没有办法使用命令’cp’复制目录并排除其中的某些文件/子目录?

使用rsync

 rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination 

请注意,使用sourcesource/是不同的。 尾部斜杠表示将文件夹source的内容复制到destination 。 如果没有尾部斜杠,则表示将文件夹源复制到destination

或者,如果要排除许多目录(或文件),可以使用--exclude-from=FILE ,其中FILE是包含要排除的文件或目录的文件的名称。

--exclude也可能包含通配符,例如--exclude=*/.svn*

复制自: https //stackoverflow.com/a/2194500/749232

如果你想使用cp本身:

 find . -type f -not -iname '*/not-from-here/*' -exec cp '{}' '/dest/{}' ';' 

这假定目标目录结构与源的相同。

复制自: https //stackoverflow.com/a/4586025/749232

在游戏的后期,但这是一个使用普通bash和cp的非常不同的解决方案:你可以使用全局文件规范,同时忽略一些文件。

假设目录包含文件:

 $ ls * listed1 listed2 listed3 listed4 unlisted1 unlisted2 unlisted3 

使用GLOBIGNORE变量:

 $ export GLOBIGNORE='unlisted*' $ ls * listed1 listed2 listed3 listed4 

或者更具体的排除:

 $ export GLOBIGNORE='unlisted1:unlisted2' $ ls * listed1 listed2 listed3 listed4 unlisted3 

或者使用否定匹配 :

 $ ls !(unlisted*) listed1 listed2 listed3 listed4 

这也支持几种不匹配的模式:

 $ ls !(unlisted1|unlisted2) listed1 listed2 listed3 listed4 unlisted3