What Are Glob Patterns and How Does Glob Wildcard Syntax Work?
Updated
Glob patterns are a compact syntax for matching filenames and paths, not arbitrary text. A pattern like *.jpg matches every file in the current directory ending in .jpg, which is why a shell command such as cp *.jpg ./thumbnails copies all JPEGs into a thumbnails folder. Use globbing when you need to select files by name or path shape; use regular expressions when you need to match or extract text content. The exact syntax depends on the implementation, so a pattern that works in one tool may behave differently in another.
Glob vs. regular expressions
Both use wildcards, but they solve different problems:
| Glob | Regular expressions | |
|---|---|---|
| Target | Files and paths | Any text |
* meaning |
Any string (often excluding /) |
Zero or more of the previous token |
| Typical use | Shell commands, .gitignore, build tools |
Search, validation, parsing |
| Readability | Short, path-oriented | Expressive, more complex |
If you are selecting files, reach for glob. If you are matching a string inside a file, reach for regex.
Core wildcards
These three symbols cover most everyday patterns:
?— matches a single character.file?.txtmatchesfile1.txtandfileA.txtbut notfile10.txt.*— matches any string, typically within a single path segment.*.jpgmatches all JPEG files in the current directory./— the path separator. It divides a pattern into segments, and in many implementations*will not cross it.
That last point matters: in most tools * stays inside one directory level, while ** is what lets you reach across directories.
Globstar: **
** matches all files and zero or more directories and subdirectories. It is the feature that makes recursive patterns practical.
**/*.jsmatches every.jsfile at any depth.- If
**is followed by/, it matches only directories and subdirectories. - For this behavior to apply,
**must be the only thing inside its path segment. A pattern like/myapp/**.jsdoes not behave the same way, because**is not alone in its segment.
Brace expansion: {a,b} and {a..c}
Braces expand one pattern into several, and the final result is the union of all of them.
**/*.{b,c}expands into**/*.band**/*.c, matching files with either extension.**/{a..c}expands into**/a,**/b, and**/c, matching those three names.
This is a shorthand for writing multiple patterns, not a matching operator in its own right.
Extended glob (extglob)
Extended glob matching adds operators that borrow from regular expression quantifiers. Each takes a group in parentheses:
| Operator | Meaning |
|---|---|
?(a|b) |
Matches zero or one occurrence |
*(a|b) |
Matches zero or more occurrences |
+(a|b) |
Matches one or more occurrences |
@(a|b) |
Matches exactly one occurrence |
!(a|b) |
Matches anything except the given pattern |
So +(a|b) matches a, b, ab, ba, and longer combinations, while @(a|b) matches only a or b. Extglob is not enabled everywhere by default, so check whether your tool supports it before relying on it.
Where globbing is used
Globbing originated in Unix shells and now appears across many tools:
- Shell commands such as
cp,ls, andrm. .gitignorefiles, which use patterns to exclude files and folders from version control.- Build tools, linters, and test runners that select source files by pattern.
- Programming languages through libraries:
minimatchandmicromatchin JavaScript, theglobmodule in Python, and shell globbing on Linux.
Syntax varies by implementation
There is no single glob standard. The same pattern can mean different things depending on the tool evaluating it. For example, the Globster tester uses the JavaScript minimatch library (version 3.0.4), which supports basic wildcards (?, *), brace expansion, extended glob matching, and globstar. Other implementations may support only a subset or add their own extensions.
When a pattern does not work as expected, the fastest way to find out why is to test it against a real file tree and see which files get highlighted. That turns a guess about syntax into a visible result.