Skip to content
Latchkey

find -regex: Match Paths With Regular Expressions

find -regex matches the entire path against a regular expression.

When globs are not expressive enough, -regex matches alternations and character classes. The catch is that it anchors against the whole path.

What it does

find -regex PATTERN matches when the regular expression matches the whole path, not just the base name and not a substring. -iregex is the case-insensitive form. On GNU find the default dialect is "emacs"; -regextype selects another, such as posix-extended.

Common usage

Terminal
find . -regex '.*\.\(js\|ts\)$'
find . -regextype posix-extended -regex '.*\.(js|ts)$'
find . -iregex '.*/readme\.md'

Options

ElementWhat it does
-regex PATTERNMatch the whole path against a regex
-iregex PATTERNCase-insensitive whole-path regex
-regextype TYPEGNU: pick the dialect (posix-extended, etc.)
.* leadingNeeded because the regex anchors at the start

In CI

Because -regex matches the entire path, you almost always need a leading .* and a trailing anchor. Under the default GNU "emacs" dialect, grouping and alternation need backslashes (\( \| \)); add -regextype posix-extended to use the more familiar ( | ) syntax.

Common errors in CI

A -regex that "matches nothing" usually forgot the leading .* and tried to match a substring; the regex must match the full path. -regextype is GNU only and must precede -regex; BSD/macOS find uses -E for extended regex instead and has no -regextype. Unescaped ( ) under the emacs dialect are literal, not grouping.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →