我想复制一个目录中的所有文件,除了特定子目录中的一些文件。 我注意到cp命令没有——exclude选项。 那么,我怎么才能做到呢?


当前回答

这是对Linus Kleen的回答的修改。他的回答对我没用,因为会有。添加在cp不喜欢的文件路径前面(路径看起来像source/.destination/file)。

这个命令对我很管用:

找到。-type f -not -path '*/ exclude -path/*' -exec cp——parents '{}' '/destination/' \;

——parents命令保留目录结构。

其他回答

另一个更简单的选项是安装并使用rsync,它有一个——exclude-dir选项,可以用于本地和远程文件。

这是对Linus Kleen的回答的修改。他的回答对我没用,因为会有。添加在cp不喜欢的文件路径前面(路径看起来像source/.destination/file)。

这个命令对我很管用:

找到。-type f -not -path '*/ exclude -path/*' -exec cp——parents '{}' '/destination/' \;

——parents命令保留目录结构。

mv tobecopied/tobeexcluded .
cp -r tobecopied dest/
mv tobeexcluded tobecopied/

rsync

rsync -r --verbose --exclude 'exclude_pattern' ./* /to/where/

首先用-n选项试试,看看会复制什么

cp -r `ls -A | grep -v "c"` $HOME/