使用 Mac 终端删除与正则表达式匹配的目录中的文件

ada*_*ign 7 shell wildcards rm

如何使用 Mac 终端删除与给定正则表达式或类似解决方案匹配的目录中的文件?

jor*_*lli 14

使用find命令。

Find all files (recursively) matching a regex: find . -type f -regex '/ex/'
Find all files (recursively) matching a regex and delete them:find . -type f -regex '/ex/' -exec rm {} \;

方括号存储找到的路径名,反斜杠转义分号,因为它被传递给 find;如果不逃脱它,它将被外壳消耗掉。如果这超出了您的理解,请阅读“学习 Bash Shell”的前两章。

查看手册页以find获取更多选项。还有更多的搜索方式。

  • 使用 `rm '{}' +` + 更像 xargs 行为或其他东西......我真的只记得它比做 `\;` 更有效率。 (2认同)