Use of the “OR” Regex operator with the find command


There are so many of these questions on the net, but I was unable to solve this simple problem.

I have a directory with a lot of images, but I only want to copy a subset of them identified by ranges of numbers. Each picture has the format: "[random characters][capture number].BMP"

For example: IZ000561.BMP

I am using this in conjunction with find and the -regex option.
The regular expression I thought to use was simple:


if I wanted to match images with a tag of [262-267],[270-272]. This approach failed, however. I went to an online Regex tester and all matched as expected using this expression. It must be that find's regex engine expects a different format for this kind of filtering.

The full command I was using:

find /path/to/images/ -regex ".*(26[2-7]|27[0-2]).*" -exec echo {} \;

What is a valid expression for what I am trying to do?

Best Answer

Reading into the man page of find gives a bit of useful information:

The regular expressions understood by find are by default Emacs Regular Expressions.

So, taking a look into the syntax of Emacs Regex

enter image description here

It seems that all special characters are required to be escaped with \. Taking this into account, the expected results were obtained after changing the regular expression to the following:

find /path/to/images/ -regex ".*\(26[2-7]\|27[0-2]\).*" -exec echo {} \;

I consulted the man page before posting this question, but apparently not meticulously enough.