Concept

Globbing

Definition

Globbing is the shell's pattern-matching mechanism for selecting filenames. When you type a command that includes a wildcard pattern, the shell expands that pattern into the actual list of matching files before passing them as arguments to the command. The asterisk matches any sequence of characters, the question mark matches exactly one character, square brackets define a character class, and the doubled asterisk (in shells that support it) matches across directory boundaries.

Globbing is older and simpler than regular expressions. It exists to make common filesystem operations terse — selecting every text file, every numbered backup, every entry in a date-based folder — without the complexity of a full regex engine.

Why it matters

How it works

When the shell parses a command line, it scans each argument for glob metacharacters. If it finds any, it walks the relevant directory or directories and tests each entry against the pattern. Matching is done left to right against the literal characters in the pattern, with the special metacharacters consuming whatever the filesystem provides. The matching set is then sorted alphabetically and substituted into the command line in place of the original pattern. By the time the kernel sees the exec call, the pattern has been replaced by a list of strings; the called program never knows globbing happened.

This pre-expansion model is the most important thing to understand about globs. It explains why commands operating on many files have no per-file invocation cost (the shell did the work), why an unquoted glob in a script can break when no files match, and why globs cannot match the contents of a file (they are filename-only, expanded before the program runs). Programs that want pattern matching against file contents need their own engine — typically a regex library — because the shell is no longer involved by then.

Where it goes next

Continue exploring

Tags