我想复制一个目录中的所有文件,除了特定子目录中的一些文件。 我注意到cp命令没有——exclude选项。 那么,我怎么才能做到呢?
当前回答
rsync
rsync -r --verbose --exclude 'exclude_pattern' ./* /to/where/
首先用-n选项试试,看看会复制什么
其他回答
cp -r `ls -A | grep -v "c"` $HOME/
我猜你用的是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的第一个参数
只需将其临时移动到一个隐藏目录中(如果需要的话,可以重新命名它)。
mkdir .hiddendir
cp * .hiddendir -R
mv .hiddendir realdirname
Rsync无法使用。下面是一个可行的替代方案。
tar -cf - --exclude='./folder' --exclude='./file.tar' ./source_directory | tar -xf - -C ./destination_directory
它相对于源目录。 这将排除目录源/。Git被复制。
rsync -r --exclude '.git' source target