我想复制一个目录中的所有文件,除了特定子目录中的一些文件。 我注意到cp命令没有——exclude选项。 那么,我怎么才能做到呢?
当前回答
扩展mvds的评论,这对我来说是可行的
cd dotfiles
tar -c --exclude .git --exclude README . | tar -x -C ~/dotfiles2
其他回答
晚了10年。感谢Linus Kleen。
我讨厌rsync!,)那么为什么不用find和cp呢?用这个答案还可以用mkdir创建一个不存在的文件夹结构。
CD /source_folder/ &&查找。-type d -not -path '*/not-from here/*' -print -exec mkdir -p '/destination_folder/{}' \;
CD /source_folder/ &&查找。-type f -not -path '*/not-from here/*' -print -exec cp -au '{}' '/destination_folder/{}' \;
看起来cd ìs必须连接相对路径与find。
Mkdir -p将创建所有子文件夹,并且当文件夹已经存在时不会报错。
侯斯顿,我们还有下一个问题。如果有人创建了一个新文件夹,中间有一个新文件,会发生什么情况?正确:对于这些新文件,它将失败。(解决方案:再运行一次!把所有东西都放在一个find命令中的解决方案似乎很难。
清理:https://unix.stackexchange.com/q/627218/239596
cp -rv `ls -A | grep -vE "dirToExclude|targetDir"` targetDir
编辑:忘记排除目标路径(否则它将递归复制)。
为什么要使用rsync,当你可以:
find . -type f -not -iname '*/not-from-here/*' -exec cp '{}' '/dest/{}' ';'
这假设目标目录结构与源目录结构相同。
rsync
rsync -r --verbose --exclude 'exclude_pattern' ./* /to/where/
首先用-n选项试试,看看会复制什么
我猜你用的是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的第一个参数