What is Glob?
Glob is a pattern matching syntax used to specify sets of filenames or paths using wildcard characters. It originated in Unix shells and is now widely used in programming languages, build tools, and file systems.
Quick Facts
| Full Name | Glob Pattern / Globbing |
|---|---|
| Created | 1971 (Unix glob command) |
| Specification | Official Specification |
How It Works
Glob patterns use special characters to match multiple files or directories. The asterisk (*) matches any sequence of characters, question mark (?) matches any single character, and brackets [] match character sets. Double asterisk (**) in extended glob syntax matches directories recursively. Glob is simpler than regular expressions but sufficient for most file matching needs. It's used in .gitignore files, build configurations, and command-line operations. Common glob patterns in .gitignore: 'node_modules/' ignores directory anywhere, '*.log' ignores all log files, '!important.log' negates previous pattern, 'build/**/temp' matches nested directories. The '**' pattern for recursive matching is a common extension beyond POSIX glob.
Key Characteristics
- * matches any sequence of characters (except path separator)
- ? matches exactly one character
- [] matches any character in the set
- ** matches directories recursively (extended glob)
- ! or ^ negates a character set
- Simpler syntax than regular expressions
Common Use Cases
- File selection in build tools (webpack, gulp)
- .gitignore patterns for excluding files
- Command-line file operations (ls *.txt)
- Test file discovery in testing frameworks
- Asset bundling and copying in build processes
Example
Loading code...Frequently Asked Questions
What is the difference between glob patterns and regular expressions?
Glob patterns are simpler and designed specifically for file path matching, using wildcards like * and ?. Regular expressions are more powerful and complex, supporting advanced pattern matching with quantifiers, groups, and lookaheads. Glob is sufficient for most file operations, while regex is needed for complex text processing.
What does the double asterisk (**) mean in glob patterns?
The double asterisk (**) is an extended glob syntax that matches directories recursively. For example, 'src/**/*.js' matches all JavaScript files in the src directory and all its subdirectories at any depth level.
How do I exclude files using glob patterns?
In most tools, you can use the exclamation mark (!) to negate a pattern. For example, in .gitignore, '*.log' followed by '!important.log' will ignore all log files except important.log. The negation pattern must come after the inclusion pattern.
Are glob patterns case-sensitive?
Glob pattern case sensitivity depends on the operating system and tool being used. On Unix/Linux systems, globs are typically case-sensitive, while on Windows they are usually case-insensitive. Many tools provide options to control case sensitivity explicitly.
What is the difference between * and ** in glob patterns?
A single asterisk (*) matches any sequence of characters within a single path segment (it won't cross directory boundaries). Double asterisk (**) matches across directory boundaries, allowing recursive matching through multiple directory levels.