Glob Patterns Cheat Sheet: Bash, .gitignore and Node fs.glob
Checked against Glob patterns Bash 5.3 manual; gitignore docs for Git 2.56.1 (tested on Git 2.50.1); Node 26.13.1 docs (tested on Node 24.4.0) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet covers the three places most developers write globs: the bash command line, .gitignore files and Node's built-in fs.glob. It follows the Bash 5.3 reference manual, the gitignore page for Git 2.56.1 and the Node 26.13.1 API docs. The gitignore and Node rows were run in a scratch folder; this shell is zsh with bash blocked, so the bash rows are checked against the manual only.
When to use what
The same characters mean slightly different things in each tool, so first ask who is doing the matching. In bash, the shell expands the pattern before the command ever runs, and an unmatched pattern is passed through as literal text. In .gitignore, Git matches paths relative to the file's folder and treats a slash as an anchor. In Node, fs.glob walks the tree itself and you get an array or async iterator back.
If you want a pattern that matches at any depth, bash needs shopt -s globstar before ** does anything special. Git and Node understand ** out of the box. Braces are different again: in bash they are brace expansion, a text substitution that happens first and doesn't care whether files exist, which is why rm {a,b}.tmp will happily name files that aren't there.
For ignore files, keep rules short and test them instead of reasoning about them. git check-ignore -v path names the exact file and line that decides a path, and the glob pattern tester lets you try a pattern against a list of paths in the browser. Starting a new repo? The gitignore generator gives you a sane base, and the .dockerignore generator does the same for build contexts.
From the Bash 5.3 manual. Not run here because the agent shell blocks bash; run bash and try them in a scratch folder.
*.txtMatch any run of characters, including none, but not a leading dot or a slash in filenames
file?.txtMatch exactly one character, so file1.txt matches and file10.txt doesn't
file[0-9].txtMatch one character from a range; the range follows your locale unless LC_ALL=C or globasciiranges is set
file[!1].txtNegate a bracket expression with ! or ^ to match any one character except those listed
[[:digit:]] [[:alpha:]]Use a POSIX character class inside brackets instead of guessing ranges
shopt -s globstar; ls src/**/*.jsTurn on ** so it matches zero or more directories; without globstar it behaves like a single *
Brace expansion rules are on https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html. It runs before every other expansion and is purely textual.
cp app.{js,map} dist/Brace expansion writes out app.js app.map whether or not the files exist
echo {01..10..3}Sequence expression with zero padding and a step: 01 04 07 10
shopt -s nullglobMake a pattern with no matches expand to nothing instead of staying as literal text
shopt -s failglobMake a pattern with no matches an error so the command doesn't run at all
shopt -s dotglobLet * match names starting with a dot; . and .. still need a pattern that starts with a dot
shopt -s extglob; ls !(*.md|*.txt)Enable extended patterns ?() *() +() @() !(); this one lists everything except Markdown and text files
Rows were run with git check-ignore -v --no-index on Git 2.50.1 in a throwaway repo. The manual has had no changes since Git 2.55.0.
*.logNo slash, so it matches at any depth: logs/err.log was ignored by this line
/README.mdLeading slash anchors to the .gitignore folder: README.md was ignored, docs/README.md was not
build/Trailing slash matches only a directory (and everything in it), not a file named build
!logs/app.logRe-include one file after *.log; check-ignore -v prints the ! line and the file is tracked again
build/* then !build/keep/Ignore the contents, not the folder, so a child can be re-included; with build/ the negation had no effect
doc/**/*.pdfMiddle ** matches zero or more folders, so doc/a.pdf and doc/x/y/a.pdf both match
\#notes.txtEscape a leading # or ! with a backslash to match it literally instead of starting a comment or negation
Precedence from the gitignore page, highest first: command-line patterns, .gitignore files (deeper wins), .git/info/exclude, then core.excludesFile.
git check-ignore -v path/to/filePrint the source file, line number and pattern that decides whether a path is ignored
git check-ignore -v --no-index pathTest a rule against a path that is already tracked, ignoring the index
git rm --cached secrets.jsonStop tracking a file that is already committed; .gitignore never affects tracked files
.git/info/excludePut personal, repo-only ignores here so they never get committed
git config --global core.excludesFile ~/.config/git/ignoreSet one global ignore file for editor and OS junk across every repo
Added in Node v22.0.0, stable since v24.0.0 and v22.17.0. path.matchesGlob is documented at https://nodejs.org/api/path.html#pathmatchesglobpath-pattern.
for await (const f of glob("**/*.js"))Iterate matches lazily with the promise API from node:fs/promises
globSync("src/**/*.js")Get an array synchronously; this returned src/x.js and src/a/y.js
globSync("*.js")Dotfiles are skipped by default: .hidden.js was not returned
globSync("**/*.log", { exclude: ["logs/err.log"] })Drop paths with glob patterns or a function; negation like !foo.js is not supported in exclude
{ cwd: "packages/api" }Run the match from another folder; it also accepts a URL since v24.1.0
path.matchesGlob("src/a/b/z.ts", "src/**/*.ts")Test a single path string against a pattern without touching the disk; returned true
Worked example: Ignore a build folder but keep one subfolder
You want everything under build/ ignored except build/keep/, which holds a checked-in fixture. The obvious attempt fails. Here is the test, run on Git 2.50.1:
$ printf 'build/\n!build/keep/\n' > .gitignore
$ git check-ignore -v --no-index build/keep/k.txt
.gitignore:1:build/ build/keep/k.txt
$ printf 'build/*\n!build/keep/\n' > .gitignore
$ git check-ignore -v --no-index build/keep/k.txt
$ echo $?
1
$ git check-ignore -v --no-index build/out.o
.gitignore:1:build/* build/out.o
In the first version, build/ excludes the directory itself. Git never looks inside an excluded directory, so the !build/keep/ line can't bring anything back, and -v points straight at line 1 as the culprit. Changing the rule to build/* ignores the children instead of the folder. Now the negation applies: check-ignore prints nothing and exits 1, meaning the fixture is not ignored, while build/out.o is still caught by line 1.
Common mistakes
Trying to re-include a file whose parent directory is already ignored.
Ignore the directory's contents with
dir/*instead ofdir/, then add the!line for the child you want back.Adding a file to
.gitignoreand expecting Git to forget it.Run
git rm --cached <file>and commit, because ignore rules only apply to untracked files.Using
**in bash scripts and getting single-level matches.Put
shopt -s globstarnear the top of the script; bash doesn't enable it by default.Looping over
*.csvin a script when the folder might be empty, and processing a file literally named*.csv.Set
shopt -s nullglobso an unmatched pattern disappears, or check that the file exists inside the loop.Writing
src/*.tsin .gitignore and expecting it to match nested files.A single star stops at slashes; use
src/**/*.ts, or drop the folder prefix so the pattern matches at any depth.
Related cheat sheets
Sources
- https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
- https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
- https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html
- https://git-scm.com/docs/gitignore
- https://git-scm.com/docs/git-check-ignore
- https://nodejs.org/api/fs.html#fspromisesglobpattern-options
- https://nodejs.org/api/path.html#pathmatchesglobpath-pattern
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.