What Is Minimatch and How Do Its Glob Patterns Work?

Updated

Minimatch is a JavaScript library that matches file paths against glob patterns — the same wildcard syntax your shell uses when you type cp *.jpg ./thumbnails. If you need to filter files in a Node.js build script, a linter config, or a .gitignore-style rule, minimatch is one of the standard ways to evaluate those patterns in JavaScript. This article covers the syntax it supports, how to test a pattern before trusting it, and when a different library might fit better.

What minimatch actually does

Globbing is pattern matching for the filesystem. A glob pattern like *.jpg describes a set of filenames, and a matcher decides whether a given path belongs to that set.

Minimatch takes two inputs — a pattern string and a path string — and returns whether they match. It does not walk the filesystem itself; it only evaluates the pattern against paths you hand it. That separation matters: the library answers "does this path match?" while a separate tool decides which paths to check.

The online glob tester at globster.xyz uses minimatch (version 3.0.4) to evaluate patterns typed into its input field, highlighting matched files in blue inside a directory tree. That makes it a practical way to see what a pattern does before you wire it into code.

Core wildcard syntax

Minimatch supports the basic wildcards plus several extended features. The table below summarizes the main tokens.

Token Meaning
? Matches a single character
* Matches any string (within a path segment)
/ Path separator
** Globstar: matches all files and zero or more directories and subdirectories
{a,b} Brace expansion into multiple patterns
?(a|b) Extglob: zero or one occurrence
*(a|b) Extglob: zero or more occurrences
+(a|b) Extglob: one or more occurrences
@(a|b) Extglob: exactly one occurrence
!(a|b) Extglob: anything except the listed patterns

How globstar behaves

** is the token people most often get wrong. It matches all files and zero or more directories and subdirectories — but only when it stands alone as a path segment. If it is followed by a /, it matches only directories and subdirectories. And it must be the only thing inside its path part: a pattern like /myapp/**.js will not behave as a recursive match, because ** is glued to .js rather than occupying its own segment. Write /myapp/**/*.js instead.

Brace expansion

Braces expand one pattern into several, and the final result is the union of all of them. So **/*.{b,c} expands into **/*.b and **/*.c — a path matches if it satisfies either. Ranges work too: **/{a..c} expands into **/a, **/b, and **/c.

Extended glob matching

The extglob forms (?(...), *(...), +(...), @(...), !(...)) let you express repetition and negation that plain * and ? cannot. They are the minimatch equivalent of regular-expression quantifiers, scoped to a set of alternatives.

Testing and debugging a pattern

Because glob syntax varies by implementation, a pattern that works in your shell may not behave the same in minimatch. Testing first saves guesswork.

  1. Write the pattern you intend to use, for example **/*.{js,ts}.
  2. Enter it into an online glob tester such as globster.xyz, which evaluates patterns with minimatch 3.0.4.
  3. Read the highlighted results — matched files appear in blue in the directory tree.
  4. Adjust and re-test if the match set is wider or narrower than expected. Common fixes: split a glued ** into its own segment, or convert a brace list into separate patterns.

The tester is most useful for two situations: debugging a pattern that does not work as intended, and experimenting with the provided examples to learn the syntax. If a pattern matches nothing, check whether ** is isolated in its segment and whether your braces are balanced.

Minimatch vs. other glob implementations

Globbing exists in nearly every language, and the syntax is not identical across them. The main alternatives:

  • JavaScript: minimatch, micromatch, extglob
  • Python: the glob module
  • Linux: shell glob

Minimatch supports basic wildcards (?, *), brace expansion, extended glob matching, and globstar. Micromatch is a related JavaScript option; the glob module is Python's built-in; shell glob is what your terminal applies before a command runs. The right choice depends on your runtime and how much of the extended syntax you need — if you only need * and ?, almost any implementation will do, but brace expansion and extglob are where implementations diverge.

Practical takeaways

  • Minimatch evaluates patterns against paths; it does not traverse the filesystem.
  • ? is one character, * is any string, ** is recursive — but only as a standalone segment.
  • Braces expand into a union of patterns; extglob adds repetition and negation.
  • Test patterns in an online tester before relying on them, since syntax differs between implementations.
  • Reach for micromatch, Python's glob, or shell glob when your environment or feature needs point that way.