0

From the shell, I'd like to recursively search a directory for a given glob pattern. But I want to use set-braces-syntax in my pattern:

find ~/path/to/dir -name '*.{h,m}'

Here I'd like to recursively find all .h and .m files located anywhere under the given path.

I gather from reading the glob documentation these 'set' patterns using curly ({) braces are called

csh(1) style brace expressions

However, my tests using bash and tcsh on macOS 12.3.1 reveal that this set-syntax is not supported by find. Reading the find man page, I don't see any mention of support for this syntax (maybe I missed it).

How can I accomplish this using this specific 'set' or 'csh-style' curly brace syntax? I'm less interested in work-arounds that use a different syntax.

4
  • 2
    … \( -name '*.h' -o -name '*.m' \). Call it a work-around, but it's a legitimate (and POSIX) way. Commented May 26, 2022 at 20:07
  • Good to know. Unfortunately, the only thing I can't give up here is the glob expression containing sets. I really want to find a way to use them specifically (it's for an API, and I find them superior to other options as far as user experience goes). Commented May 27, 2022 at 4:12
  • "the only thing I can't give up here is the glob expression containing sets" – Please make sure there is no XY problem here. Edit the question if there is. Commented May 27, 2022 at 4:19
  • 1
    In Bash: shopt -s globstar nullglob; for f in ./**/*.{h,m}; do printf '%s\n' "$f"; done. This is not an answer because from the title I deduce find is required. Commented May 27, 2022 at 21:42

1 Answer 1

-1

csh is "c-shell" Curly braces are generally known as array delimiters and not limited to, nor defined by csh, they form part of regular expressions They may need "escaping" for your shell to parse them properly. (A backslash ahead of them, ie: \{...\}

Maybe this help:

https://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces

3
  • Hm, this is glob syntax, not regular expressions. In this case, escaping the braces does not seem to solve the issue for me. This also returns no output: find /path/to/dir -name '*.\{h,m\}' Commented May 26, 2022 at 19:18
  • mmmm. no. glob is limited to character matching and square brackets. I'm not sure how bash on mac parses things. You could need to escape or abandon the single-ticks, or swap them for quotation marks, or something else. You might need to use the -iname switch on find.
    – mitts
    Commented May 27, 2022 at 6:40
  • You are mistaken. Glob syntax can sometimes support this "set" or "braces" syntax. I provided a link above to the man page for glob. Look under ˋGLOB_BRACEˋ. It is documented there. man7.org/linux/man-pages/man3/glob.3.html Commented May 27, 2022 at 16:54

You must log in to answer this question.

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