Zsh 排除rsync要复制的具有特定扩展名集的文件

Zsh 排除rsync要复制的具有特定扩展名集的文件,zsh,rsync,Zsh,Rsync,问题:从Zsh脚本调用rsync,我想告诉rsync排除具有特定扩展名集之一的文件。我现在的做法是: rsync -r --exclude '*.bak' --exclude '*.save' --exclude '*.backup' --exclude '*.old' ... from_directory/* to_directory 这是乏味的编写和命令行变得相当长。我试着另作选择 # THIS DOES NOT WORK: rsync -r --exclude '*.bak' --exc

问题:从Zsh脚本调用
rsync
,我想告诉rsync排除具有特定扩展名集之一的文件。我现在的做法是:

rsync -r --exclude '*.bak' --exclude '*.save' --exclude '*.backup' --exclude '*.old' ... from_directory/* to_directory
这是乏味的编写和命令行变得相当长。我试着另作选择

# THIS DOES NOT WORK:
rsync -r --exclude '*.bak' --exclude '*.{save,backup,old,...}' from_directory/* to_directory
但这不起作用-
rsync
无法使用大括号处理快捷方式(这并不奇怪,因为即使在shell级别,这也不是文件全局处理的一部分,而是在命令行解释的早期阶段发生的)

我还考虑将所有要排除的模式写入一个文件,并使用
--exclude list
而不是
--exclude
。这会起作用,但我不喜欢这个解决方案,因为我希望我的脚本是自包含的,即文件模式在脚本中可见,而不是在单独的文件中

当然,始终有一种解决方案,可以使用HERE文档创建包含扩展名的临时文件:

cat <<<LIST >excluded.txt
*.bak
*.save
... etc
LIST
rsync -r --exclude-list excluded.txt ....
rm excluded.txt

cat由于大括号扩展(
foo{a,b,c}条
→ <代码>fooabar foobbar foocbar
)不适用于带引号的大括号

请尝试以下方法

rsync -r --exclude='*'.{bak,save,backup,old} from_directory/* to_directory
诀窍是只引用
*
,并使用
--exclude
选项的值也可以用
=
而不是空格分隔。这样
zsh
就可以将它识别为一个单词,并且

--exclude='*'.{bak,save,backup,old}
将扩展到

--exclude='*'.bak --exclude='*'.save --exclude='*'.backup --exclude='*'.old