Bash: 查找中的编号文件

Bash: numbered files in Find

我正在编写一个 bash 脚本,用于查找和删除 blender 在您编辑和保存 .blend 文件时创建的 .blend1 和 .blend2 文件。它包含成对的行,如下所示:

find Documents -name "*.blend1" -exec rm -rf {} \;
find Documents -name "*.blend2" -exec rm -rf {} \;

这很好用,尽管我很好奇是否有可能以某种方式组合这两个 find 命令,这样它就只是一个命令来查找和删除 .blend1 和 .blend2 文件。

不是特别重要,如果我的脚本更紧凑一点,我会更喜欢它。

是的,您可以使用正则表达式将多个模式匹配为一个:

find Documents -regextype posix-egrep -regex '.*\.(blend1|blend2)$' -exec rm -rf {} \;

或较新的 find 版本:

find Documents -regextype posix-egrep -regex '.*\.(blend1|blend2)$' -delete

没有正则表达式你可以这样做:

find Documents \( -name "*.blend1" -o -name "*.blend2" \) -delete
find Documents -name '*.blend[12]' -delete

-delete 是 GNU 查找扩展。)

其他方式:

find Documents '(' -name '*.blend1' -o -name '*.blend2' ')' -delete
find Documents -name '*.blend*' -delete

这是另一种方式...

find Documents -name '*.blend[12]' -exec rm -rf {} \;