我想复制一个目录中的所有文件,除了特定子目录中的一些文件。 我注意到cp命令没有——exclude选项。 那么,我怎么才能做到呢?
当前回答
mv tobecopied/tobeexcluded .
cp -r tobecopied dest/
mv tobeexcluded tobecopied/
其他回答
Rsync又快又简单:
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude
你可以使用-排除多次。
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude
注意——exclude选项后的目录thefoldertoexclude是相对于源文件夹的,即:sourcefolder/thefoldertoexclude。
此外,您还可以添加-n进行演练,以查看在执行实际操作之前将复制什么,如果一切正常,则从命令行中删除-n。
另一个更简单的选项是安装并使用rsync,它有一个——exclude-dir选项,可以用于本地和远程文件。
Rsync实际上相当棘手。要做多次测试才能起作用
假设你想复制/var/www/html到/var/www/dev,但可能由于/var/www/html/site/video/目录的大小需要排除。命令如下:
Rsync -av—排除'sites/video' /var/www/html/ /var/www/dev
一些警告:
源文件中的最后一个斜杠/是必须的,否则它也会复制源目录,而不是它的内容,变成/var/www/dev/html/xxxx,这可能不是你想要的。 ——exclude路径直接相对于源。即使你使用完全的绝对路径,它也不会起作用。 -v表示详细,-a表示存档模式这意味着你想要递归,想要保存几乎所有东西。
cp -r `ls -A | grep -v "Excluded_File_or_folder"` ../$target_location -v
cp -rv `ls -A | grep -vE "dirToExclude|targetDir"` targetDir
编辑:忘记排除目标路径(否则它将递归复制)。