Path matching
Glob Patterns Explained With Matching Examples
Learn glob pattern syntax for stars, question marks, character sets and recursive matching, with practical path examples and common mistakes.
A glob selects filenames or paths with compact wildcard syntax. It is commonly used by shells, build tools and ignore files. Glob syntax is not identical to regular expressions, and exact behavior varies by application.
What a glob pattern does
A glob selects filenames or paths with compact wildcard syntax. It is commonly used by shells, build tools and ignore files. Glob syntax is not identical to regular expressions, and exact behavior varies by application.
Common patterns
| Pattern | Matches | Does not match |
|---|---|---|
*.jpg | photo.jpg | photo.png |
file?.txt | file1.txt | file10.txt |
img[0-9].png | img7.png | imgA.png |
src/**/*.js | nested JS paths | behavior varies by engine |
Stars and path separators
A single star usually matches characters within one path segment. A double star is often used for recursive directories, but some engines require an option or interpret it differently. Test against real representative paths.
Avoiding costly mistakes
Start with a preview, especially before deleting, moving or publishing files. Check case sensitivity, hidden files, separators and whether the tool matches the entire string. Quote shell patterns when the application, rather than the shell, must receive them unchanged.
Ignore files and negated patterns
Gitignore-style files add rules such as a leading exclamation mark to re-include a path and a trailing slash for directories. Order can matter because a later rule may override an earlier one. The consuming tool’s documentation is authoritative.
Test a wanted file, unwanted file, nested path, similar extension and case variation. For security boundaries, use the application’s supported path library rather than an informal wildcard check.
Quick answers
Frequently asked questions
Is a glob the same as a regex?
No. Both match text, but they use different syntax and capabilities.
What does the question mark match?
Usually exactly one character within a path segment.
Why does double star not recurse?
The specific engine may not enable globstar behavior or may use a different rule.
Are globs case-sensitive?
It depends on the tool and filesystem. Test the intended environment.
Continue with a tool