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


当前回答

为什么要使用rsync,当你可以:

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

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

其他回答

扩展mvds的评论,这对我来说是可行的

cd dotfiles
tar -c --exclude .git --exclude README . | tar -x -C ~/dotfiles2

rsync

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

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

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

为什么要使用rsync,当你可以:

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

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

cp -rv `ls -A | grep -vE "dirToExclude|targetDir"` targetDir

编辑:忘记排除目标路径(否则它将递归复制)。