1 Basic concepts

1.1 Definition of globbing

Globbing is a form of pattern matching used to identify names that fit a wildcard expression. In computing, it most often applies to filenames and paths, though it can also be used for arbitrary strings. A glob pattern typically contains special symbols that stand in for one or more characters, allowing a single expression to represent many possible matches.

1.2 Purpose and typical uses

The main purpose of globbing is to reduce repetition when referring to multiple items. Instead of listing each file separately, a user or program can specify a pattern that expands to every matching name. This makes globbing useful in command-line work, scripting, automation, and batch processing. It is especially convenient when handling groups of files with similar names or extensions.

1.3 Relationship to pattern matching

Globbing is a specialized kind of pattern matching. Unlike more expressive matching systems, it usually relies on a small set of wildcard operators and simple character rules. Its syntax is designed for quick selection rather than detailed text analysis. In many systems, glob patterns are interpreted directly by the shell or by a library function before a command or program receives the resulting list of matches.

2 Common glob syntax

2.1 Wildcards

Wildcards are symbols that match variable text. They form the core of most glob patterns and are used to describe broad sets of names with compact expressions. The exact behavior can vary slightly between systems, but the general idea remains consistent.

2.1.1 Asterisk

The asterisk matches zero or more characters. It is commonly used to select all files in a directory, such as names ending in a particular extension or beginning with a shared prefix. Because it is broad, the asterisk is one of the most frequently used glob symbols.

2.1.2 Question mark

The question mark matches exactly one character. It is useful when a name differs by a single position or when a fixed-length pattern is needed. For example, it can distinguish between similarly named files where only one character varies.

2.2 Character classes and ranges

2.2.1 Bracket expressions

Bracket expressions match one character from a specified set. A pattern such as [abc] can match any one of the listed letters, while a range such as [a-z] matches any lowercase letter in that interval. These expressions are helpful when a filename or string may contain one of several known characters at a particular position.

2.2.2 Negated character classes

Negated character classes match any single character not included in the bracketed set. They are often written with a leading exclamation mark or caret, depending on the system. This makes it possible to exclude certain characters while still allowing a broad range of alternatives.

2.3 Literal characters and escaping

Most ordinary characters in a glob pattern are treated literally, meaning they must appear exactly as written. When a character has special meaning, escaping may be required to match it as plain text. Escaping rules differ by shell or library, but the goal is always to prevent the pattern engine from interpreting a symbol as a wildcard.

3 Shell and command-line behavior

3.1 Filename expansion

In many shells, globbing happens before a command is executed. The shell scans the current directory or path context, finds names that match the pattern, and replaces the pattern with the matching results. This process is often called filename expansion or pathname expansion. It allows commands to operate on multiple files without requiring each name to be entered manually.

3.2 Shell expansion order

Globbing is only one step in a shell’s parsing process. Other expansions, such as variable substitution or command substitution, may occur before or after pattern interpretation depending on the environment. The ordering matters because it affects what text is ultimately treated as a glob and what is passed through unchanged.

3.3 Handling of unmatched patterns

If no names match a glob expression, different systems behave differently. Some shells leave the pattern unchanged, while others treat it as an empty result or generate an error. This variation can affect scripts, especially when a command expects at least one argument. Careful handling is often needed to avoid accidental misuse of unmatched patterns.

3.4 Quoting and preventing expansion

Quoting a pattern usually prevents the shell from expanding it. This is important when the user wants a literal asterisk, question mark, or bracket expression to be passed to a program. By enclosing the pattern in quotes or escaping special characters, the text can be preserved exactly as typed.

4 Variations across systems

4.1 Unix-like shells

Unix-like shells commonly provide globbing as a built-in feature. Patterns are expanded by the shell itself before the target program runs. Typical syntax includes *, ?, and bracket expressions, along with optional extensions in more advanced shells. This behavior has become a standard part of command-line use in Unix-style environments.

4.2 Windows and command processors

Windows environments have historically handled globbing differently from Unix-like shells. Some command processors do not expand wildcards automatically, leaving that task to individual programs. As a result, software written for Windows may implement its own pattern handling or depend on libraries to interpret glob expressions.

4.3 Differences between shells and programming libraries

Shell globbing and library-based globbing are similar in purpose but not always identical in detail. A shell may apply special rules for quoting, dotfiles, or path separators, while a library may follow its own implementation choices. These differences matter when the same pattern is used in scripts and in application code, since the observed matches may not be the same.

5 Programming language support

5.1 Glob libraries and APIs

Many programming languages include glob functions or libraries that search for filenames matching a pattern. These APIs often return a list of paths that satisfy the expression. They are widely used in scripts, build tools, and applications that need to process files by naming convention rather than by explicit reference.

5.2 Recursive matching

Some glob implementations support recursive search through directory trees. This allows a pattern to match files in nested folders without writing separate expressions for each level. Recursive matching is useful for projects with deep directory structures, though the exact syntax and behavior differ among systems.

5.3 Filtering strings versus file paths

Globbing may be applied either to file paths or to ordinary strings. When used for paths, directory separators and platform conventions can influence what counts as a match. When used for text, the pattern engine may ignore filesystem-specific rules and simply compare characters. The distinction is important because the same pattern can behave differently depending on its input type.

5.4 Common implementation details

Implementations usually translate a glob pattern into internal matching logic that checks candidates one by one. Some systems convert patterns into automata or equivalent search structures, while others use direct recursive comparison. Efficient handling of directories, character classes, and recursive wildcards is a common concern in these implementations.

6 Advanced glob features

6.1 Recursive wildcards

Recursive wildcards extend ordinary globbing to match across multiple directory levels. A common form is a special token that can represent one or more path segments. This feature is especially helpful when searching large projects or recursively collecting files with a shared extension or prefix.

6.2 Extglob patterns

Extglob patterns add more expressive operators to standard glob syntax. They may support grouping, alternation, or repetition-like matching while still remaining simpler than regular expressions. Such extensions are popular in some shells and tools because they offer greater flexibility without requiring a full regex syntax.

6.3 Brace expansion

Brace expansion creates multiple patterns from a compact expression. For example, a brace list can generate several alternative names or suffixes from one input. Although often discussed alongside globbing, brace expansion is a separate mechanism in many systems because it produces text combinations before pattern matching occurs.

6.4 Hidden files and dotfile rules

Many systems treat names beginning with a dot specially, especially on Unix-like platforms. Standard globs often exclude such hidden files unless the pattern explicitly begins with a dot or a special option is enabled. These rules help prevent accidental matching of configuration files and other hidden entries.

7 Practical applications

7.1 File selection in scripts

Scripts frequently use glob patterns to gather groups of files for copying, deleting, archiving, or processing. This makes automation simpler because the script can adapt to changing filenames without needing a fixed list. Globbing is particularly useful for tasks that must operate on all files of a certain type.

7.2 Build and deployment tools

Build systems and deployment tools often rely on globbing to include source files, configuration files, or assets. A pattern can define the scope of a compilation step or packaging operation. This reduces manual maintenance and helps tools respond to project growth with minimal configuration changes.

7.3 Search and batch processing

Globbing is also common in utilities that scan many files at once. Search tools may accept glob patterns to limit input sets, while batch processors use them to target collections of documents or logs. This makes it easier to manage large datasets by name rather than by path enumeration.

7.4 Log and data file handling

Operational workflows often organize logs and data outputs by naming convention. Globs make it easy to select files by date, type, or versioned suffix. As a result, they are frequently used in cleanup jobs, archival routines, and data pipelines that process rotating or periodically generated files.

8 Limitations and edge cases

8.1 Ambiguous matches

A glob pattern can match many names, and the resulting set may be broader than intended. Similar filenames, unexpected directory contents, or recursive patterns can all produce ambiguous results. Because of this, patterns should be chosen carefully to avoid selecting the wrong files.

8.2 Performance considerations

Large directories and deep path structures can make glob expansion expensive. Some patterns require extensive scanning to determine matches, especially when recursive wildcards are involved. Performance may become a concern in scripts or applications that evaluate many patterns repeatedly.

8.3 Security and injection concerns

If glob patterns are built from untrusted input, they may be used in unintended ways. A malicious or malformed pattern can widen the search scope, trigger excessive matching, or interact poorly with command execution. Safe handling usually requires validation, quoting, or avoidance of shell interpretation when patterns come from external sources.

8.4 Portability issues

Glob syntax is not perfectly uniform across platforms and tools. Differences in path separators, dotfile handling, recursive matching, and escaping can cause the same pattern to behave differently in different environments. Portable code often needs to account for these variations explicitly.

9.1 Regular expressions

Regular expressions are a more powerful pattern language used for matching text. They support richer operators, grouping, and quantifiers than typical globbing. Because of this, regexes are often used when glob patterns are too limited for a task.

9.2 Filename expansion

Filename expansion is the shell process that replaces a glob pattern with the matching filenames. It is closely associated with globbing and is one of the most common ways the term is encountered in command-line computing.

9.3 Pathname matching

Pathname matching refers to matching directory and file paths against a pattern. It is a common use case for globbing, particularly when the pattern needs to account for directory structure as well as names.