Skip to content

Menu

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.

*.txt

Match any run of characters, including none, but not a leading dot or a slash in filenames

Try it: glob-pattern-testerFrom the docs, not run by us
file?.txt

Match exactly one character, so file1.txt matches and file10.txt doesn't

Try it: glob-pattern-testerFrom the docs, not run by us
file[0-9].txt

Match one character from a range; the range follows your locale unless LC_ALL=C or globasciiranges is set

From the docs, not run by us
file[!1].txt

Negate a bracket expression with ! or ^ to match any one character except those listed

From the docs, not run by us
[[:digit:]] [[:alpha:]]

Use a POSIX character class inside brackets instead of guessing ranges

From the docs, not run by us
shopt -s globstar; ls src/**/*.js

Turn on ** so it matches zero or more directories; without globstar it behaves like a single *

From the docs, not run by us

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

From the docs, not run by us
echo {01..10..3}

Sequence expression with zero padding and a step: 01 04 07 10

From the docs, not run by us
shopt -s nullglob

Make a pattern with no matches expand to nothing instead of staying as literal text

From the docs, not run by us
shopt -s failglob

Make a pattern with no matches an error so the command doesn't run at all

From the docs, not run by us
shopt -s dotglob

Let * match names starting with a dot; . and .. still need a pattern that starts with a dot

From the docs, not run by us
shopt -s extglob; ls !(*.md|*.txt)

Enable extended patterns ?() *() +() @() !(); this one lists everything except Markdown and text files

From the docs, not run by us

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.

*.log

No slash, so it matches at any depth: logs/err.log was ignored by this line

/README.md

Leading 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.log

Re-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/**/*.pdf

Middle ** matches zero or more folders, so doc/a.pdf and doc/x/y/a.pdf both match

From the docs, not run by us
\#notes.txt

Escape a leading # or ! with a backslash to match it literally instead of starting a comment or negation

From the docs, not run by us

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/file

Print the source file, line number and pattern that decides whether a path is ignored

git check-ignore -v --no-index path

Test a rule against a path that is already tracked, ignoring the index

git rm --cached secrets.json

Stop tracking a file that is already committed; .gitignore never affects tracked files

From the docs, not run by us
.git/info/exclude

Put personal, repo-only ignores here so they never get committed

From the docs, not run by us
git config --global core.excludesFile ~/.config/git/ignore

Set one global ignore file for editor and OS junk across every repo

From the docs, not run by us

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

From the docs, not run by us
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 of dir/, then add the ! line for the child you want back.

  • Adding a file to .gitignore and 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 globstar near the top of the script; bash doesn't enable it by default.

  • Looping over *.csv in a script when the folder might be empty, and processing a file literally named *.csv.

    Set shopt -s nullglob so an unmatched pattern disappears, or check that the file exists inside the loop.

  • Writing src/*.ts in .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.