How does one implement an OR selection via regular expressions. For example, if I have the following files
/foo/bar/
├── foo1.txt
├── foo2.txt
├── foo3.txt
├── foo1.eps
├── foo2.eps
├── foo3.eps
├── foo1.pdf
├── foo2.pdf
└── foo3.pdf
and I want to specify that I want only the ones with 1 or 2 in them, I can do *[12]* like so
find . -name "*[12]*" -type f -maxdepth 1 -exec echo {} ";"
But what if I want more complex selection by only selecting the txt and eps files like so
find . -name "*[txt || eps]*" -type f -maxdepth 1 -exec echo {} ";"
Obviously the above example doesn’t work, but what I mean is that I want an OR operator || here.
The
-namepatterns are not regexes. If they were, using|would work. What works, however, is using find’s expression language to one’s advantage. Like so:You use
-oto form an or-expression and parentheses (escaped for the shell) for grouping.