For example, this doesn't find the file:

: > 12; basename $(find -E . -regex '^(.*)(\d{2})' -print 2>/dev/null)

But this does:

basename $(find -E . -regex '^(.*)([0-9]{2})' -print 2>/dev/null)

At the same time, for example:

echo -e '12\nabc\n456' | grep -E -e '^(\d{3})'

works quite well, though seemingly uses the same regcomp().

What am I doing wrong and how can I fix it?

  • 1
    Newer OS X grep use a BSD code base, not GNU grep. The -E option is a mosh of POSIX ERE and PCRE and I believe exclusive to OS X. find uses BRE and ERE and the \d character class is invalid.
    – fd0
    Commented Sep 29, 2016 at 20:40
  • @fd0 Best comment!
    – cave-canem
    Commented Oct 3, 2016 at 4:00

2 Answers 2


It appears there is a bug in the native MacOS find in which its -regex option does not correctly handle any of the Shortcuts described in man re_format. The following command seems to work:

find -E . -regex '^(.*)([[:digit:]]{2})' -print

This slightly simpler variant also seems to work:

find -E . -regex '.*[[:digit:]]{2}' -print

In short, grep -E and finds -regex option don't seem to handle regular expressions in exactly the same way.

  • @Erik Fuller, I think it's about something different, as well as the gfind . -regextype 'egrep' -regex '^(.*)(\d{2})' -print 2>/dev/null does not work
    – cave-canem
    Commented Sep 29, 2016 at 17:18
  • It's possible gfind has the same bug. In any case there seems to be a viable workaround. Commented Sep 29, 2016 at 18:06
  • the problem is that drops out the whole class of "enhanced basic and enhanced extended REs". In this case it is not clear why "egrep" is working properly, because it also uses the same regcomp ()
    – cave-canem
    Commented Sep 29, 2016 at 20:06
  • It drops out some of the extended REs, but not all of them; the [[:digit:]] construct described in this answer does not seem to work in find without -E. Commented Sep 29, 2016 at 20:09
  • Not extended REs, but "enhanced basic and enhanced extended REs"
    – cave-canem
    Commented Sep 29, 2016 at 20:55

ENHANCED FEATURES is not available in the current implementation of find, as in the compiled version of find REG_ENHANCED flag is non passed (See regex(3) and https://opensource.apple.com/source/shell_cmds/shell_cmds-187/find/main.c).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .