4

I'm trying to create a -regex expression for find on macOS.

find /Users/gerben/Library -regex '.*\(cisco\).*' -print 2>/dev/null

works, and so does

find /Users/gerben/Library -regex '.*\(citrix\).*' -print 2>/dev/null

in the sense that they both find files. Now I try

find /Users/gerben/Library -regex '.*\(cisco|citrix\).*' -print 2>/dev/null

and nothing is found. Nor does

find /Users/gerben/Library -regex '.*\(cisco\|citrix\).*' -print 2>/dev/null

nor any other variation with {} escapes and what not that I have tried. What am I doing wrong?

1 Answer 1

7

You can use -E flag, from man:

-E Interpret regular expressions followed by -regex and -iregex primaries as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats.

In this case your find command will look like:

find -E ~/Library -regex '.*(citrix|cisco).*' -print 2>/dev/null
2
  • Silly me. I was using re_format and has missed the necessity of the -E flag for that. Thanks.
    – gctwnl
    Commented Jan 26, 2019 at 20:52
  • I just found out that on MacOS, "find" considers a? (meaning 0 or 1 instances of the character a) to be a modern RE feature. If I want to find files ending in .ts or .tsx, find -regex '.*\.tsx*' works (but finds files ending in .tsxx, etc.) but find -regex '.*\.tsx?' does not work without -E.
    – ajb
    Commented Apr 6, 2023 at 18:15

You must log in to answer this question.

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