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


当前回答

我猜你用的是bash或dash。这样能行吗?

shopt -s extglob  # sets extended pattern matching options in the bash shell
cp $(ls -laR !(subdir/file1|file2|subdir2/file3)) destination

执行ls,排除你不想要的文件,并使用它作为cp的第一个参数

其他回答

它相对于源目录。 这将排除目录源/。Git被复制。

rsync -r --exclude '.git' source target

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

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

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

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

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

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

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