如何设置 gnu find 使用 posix-extended regex 类型作为默认值

how to set gnu find use posix-extended regex type as default

当我使用 find regex 查找 .c .cpp .h 文件时 我必须输入

find . -regex ".*\.\(c\|cpp\|h)"

或使用 posix-扩展正则表达式类型

find . -regextype posix-extended -regex ".*\.(c|cpp)"

第一个有很多'\',不太好读。 第二个必须输入更多的字符。而第二个我比较熟悉

有什么方法可以让 find 默认使用 posix 扩展正则表达式吗?

我尝试设置别名

alias find='find -regextype posix-extended'

在我的 .zshrc 文件中。但它不起作用,因为 find 需要将路径放在第二个参数上。

感谢任何建议。

对于 zsh,您有几个选择。您可以定义一个全局别名:

alias -g reg="-regextype posix-extended"

这将允许您输入 find ./ reg -regex ".*\.(c|cpp)",zsh 会为您替换。

另一种选择是创建函数。类似于:

function findr()
{
   dir=;
   shift;
   find $dir -regextype posix-extended $*
}

可以这样调用:

findr ./ -regex ".*\.(c|cpp)"