Glob
Match files using the patterns the shell uses, like stars and stuff.
This is a glob implementation in JavaScript. It uses the minimatch library to do its matching.

Usage
Glob Primer
"Globs" are the patterns you type when you do stuff like ls *.js on the command line, or put build/* in a .gitignore file.
Before parsing the path part patterns, braced sections are expanded into a set. Braced sections start with { and end with }, with any number of comma-delimited sections within. Braced sections may contain slash characters, so a{/b/c,bcd} would expand into a/b/c and abc.
The following characters have special magic meaning when used in a path portion:
*Matches 0 or more characters in a sinle path portion?Matches 1 character[...]Matches a range of characters, similar to a RegExp range. If the first character of the range is!or^then it matches any character not in the range.!(pattern|pattern|pattern)Matches anything that does not match any of the patterns provided.?(pattern|pattern|pattern)Matches zero or one occurrence of the patterns provided.+(pattern|pattern|pattern)Matches one or more occurrences of the patterns provided.*(a|b|c)Matches zero or more occurrences of the patterns provided@(pattern|pat*|pat?erN)Matches exactly one of the patterns provided**If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories.
Dots
If a file or directory path portion has a . as the first character, then it will not match any glob pattern unless that pattern's corresponding path part also has a . as its first character.
For example, the pattern a/.*/c would match the file at a/.b/c. However the pattern a/*/c would not, because * does not start with a dot character.
You can make glob treat dots as normal characters by setting dot:true in the options.
Basename Matching
If you set matchBase:true in the options, and the pattern has no slashes in it, then it will seek for any file anywhere in the tree with a matching basename. For example, *.js would match test/simple/basic.js.
Negation
The intent for negation would be for a pattern starting with ! to match everything that doesn't match the supplied pattern. However, the implementation is weird, and for the time being, this should be avoided. The behavior will change or be deprecated in version 5.
Empty Sets
If no matching files are found, then an empty array is returned. This differs from the shell, where the pattern itself is returned. For example:
To get the bash-style behavior, set the nonull:true in the options.
See Also:
man shman bash(Search for "Pattern Matching")man 3 fnmatchman 5 gitignore
glob.hasMagic(patter, [options])
Returns true if there are any special characters in the pattern, and false otherwise.
Note that the options affect the results. If noext:true is set in the options object, then +(a|b) will not be considered a magic pattern. If the pattern has a brace expansion, like a/{b/c,x/y} then that is considered magical, unless nobrace:true is set in the options.
glob(pattern, [options], cb)
pattern{String} Pattern to be matchedoptions{Object}cb{Function}err{Error | null}matches{Array} filenames found matching the pattern
Perform an asynchronous glob search.
glob.sync(pattern, [options])
pattern{String} Pattern to be matchedoptions{Object}return: {Array} filenames found matching the pattern
Perform a synchronous glob search.
Class: glob.Glob
Create a Glob object by instanting the glob.Glob class.
It's an EventEmitter, and starts walking the filesystem to find matches immediately.
new glob.Glob(pattern, [options], [cb])
pattern{String} pattern to search foroptions{Object}cb{Function} Called when an error occurs, or matches are founderr{Error | null}matches{Array} filenames found matching the pattern
Note that if the sync flag is set in the options, then matches will be immediately available on the g.found member.
Properties
minimatchThe minimatch object that the glob uses.optionsThe options object passed in.abortedBoolean which is set to true when callingabort(). There is no way at this time to continue a glob search after aborting, but you can re-use the statCache to avoid having to duplicate syscalls.statCacheCollection of all the stat results the glob search performed.cacheConvenience object. Each field has the following possible values:false- Path does not existtrue- Path exists'DIR'- Path exists, and is not a directory'FILE'- Path exists, and is a directory[file, entries, ...]- Path exists, is a directory, and the array value is the results offs.readdir
statCacheCache offs.statresults, to prevent statting the same path multiple times.symlinksA record of which paths are symbolic links, which is relevant in resolving**patterns.
Events
endWhen the matching is finished, this is emitted with all the matches found. If thenonulloption is set, and no match was found, then thematcheslist contains the original pattern. The matches are sorted, unless thenosortflag is set.matchEvery time a match is found, this is emitted with the matched.errorEmitted when an unexpected error is encountered, or whenever any fs error occurs ifoptions.strictis set.abortWhenabort()is called, this event is raised.
Methods
pauseTemporarily stop the searchresumeResume the searchabortStop the search forever
Options
All the options that can be passed to Minimatch can also be passed to Glob to change pattern matching behavior. Also, some have been added, or have glob-specific ramifications.
All options are false by default, unless otherwise noted.
All options are added to the Glob object, as well.
If you are running many glob operations, you can pass a Glob object as the options argument to a subsequent operation to shortcut some stat and readdir calls. At the very least, you may pass in shared symlinks, statCache, and cache options, so that parallel glob operations will be sped up by sharing information about the filesystem.
cwdThe current working directory in which to search. Defaults toprocess.cwd().rootThe place where patterns starting with/will be mounted onto. Defaults topath.resolve(options.cwd, "/")(/on Unix systems, andC:\or some such on Windows.)dotInclude.dotfiles in normal matches andglobstarmatches. Note that an explicit dot in a portion of the pattern will always match dot files.nomountBy default, a pattern starting with a forward-slash will be "mounted" onto the root setting, so that a valid filesystem path is returned. Set this flag to disable that behavior.markAdd a/character to directory matches. Note that this requires additional stat calls.nosortDon't sort the results.statSet to true to stat all results. This reduces performance somewhat, and is completely unnecessary, unlessreaddiris presumed to be an untrustworthy indicator of file existence.silentWhen an unusual error is encountered when attempting to read a directory, a warning will be printed to stderr. Set thesilentoption to true to suppress these warnings.strictWhen an unusual error is encountered when attempting to read a directory, the process will just continue on in search of other matches. Set thestrictoption to raise an error in these cases.cacheSeecacheproperty above. Pass in a previously generated cache object to save some fs calls.statCacheA cache of results of filesystem information, to prevent unnecessary stat calls. While it should not normally be necessary to set this, you may pass the statCache from one glob() call to the options object of another, if you know that the filesystem will not change between calls. (See "Race Conditions" below.)symlinksA cache of known symbolic links. You may pass in a previously generatedsymlinksobject to savelstatcalls when resolving**matches.syncPerform a synchronous glob search.nouniqueIn some cases, brace-expanded patterns can result in the same file showing up multiple times in the result set. By default, this implementation prevents duplicates in the result set. Set this flag to disable that behavior.nonullSet to never return an empty set, instead returning a set containing the pattern itself. This is the default in glob(3).nocasePerform a case-insensitive match. Note that case-insensitive filesystems will sometimes result in glob returning results that are case-insensitively matched anyway, since readdir and stat will not raise an error.debugSet to enable debug logging in minimatch and glob.nobraceDo not expand{a,b}and{1..3}brace sets.noglobstarDo not match**against multiple filenames. (Ie, treat it as a normal*instead.)noextDo not match+(a|b)"extglob" patterns.nocasePerform a case-insensitive match. Note: on case-insensitive filesystems, non-magic patterns will match by default, sincestatandreaddirwill not raise errors.matchBasePerform a basename-only match if the pattern does not contain any slash characters. That is,*.jswould be treated as equivalent to**/*.js, matching all js files in all directories.nonegateSuppressnegatebehavior. (See below.)nocommentSuppresscommentbehavior. (See below.)nonullReturn the pattern when no matches are found.nodirDo not match directories, only files.
Comparisons to other fnmatch/glob implementations
While strict compliance with the existing standards is a worthwhile goal, some discrepancies exist between node-glob and other implementations, and are intentional.
If the pattern starts with a ! character, then it is negated. Set the nonegate flag to suppress this behavior, and treat leading ! characters normally. This is perhaps relevant if you wish to start the pattern with a negative extglob pattern like !(a|B). Multiple ! characters at the start of a pattern will negate the pattern multiple times.
If a pattern starts with #, then it is treated as a comment, and will not match anything. Use \# to match a literal # at the start of a line, or set the nocomment flag to suppress this behavior.
The double-star character ** is supported by default, unless the noglobstar flag is set. This is supported in the manner of bsdglob and bash 4.3, where ** only has special significance if it is the only thing in a path part. That is, a/**/b will match a/x/y/b, but a/**b will not.
Note that symlinked directories are not crawled as part of a **, though their contents may match against subsequent portions of the pattern. This prevents infinite loops and duplicates and the like.
If an escaped pattern has no matches, and the nonull flag is set, then glob returns the pattern as-provided, rather than interpreting the character escapes. For example, glob.match([], "\\*a\\?") will return "\\*a\\?" rather than "*a?". This is akin to setting the nullglob option in bash, except that it does not resolve escaped pattern characters.
If brace expansion is not disabled, then it is performed before any other interpretation of the glob pattern. Thus, a pattern like +(a|{b),c)}, which would not be valid in bash or zsh, is expanded first into the set of +(a|b) and +(a|c), and those patterns are checked for validity. Since those two are valid, matching proceeds.
Windows
Please only use forward-slashes in glob expressions.
Though windows uses either / or \ as its path separator, only / characters are used by this glob implementation. You must use forward-slashes only in glob expressions. Back-slashes will always be interpreted as escape characters, not path separators.
Results from absolute patterns such as /foo/* are mounted onto the root setting using path.join. On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
Race Conditions
Glob searching, by its very nature, is susceptible to race conditions, since it relies on directory walking and such.
As a result, it is possible that a file that exists when glob looks for it may have been deleted or modified by the time it returns the result.
As part of its internal implementation, this program caches all stat and readdir calls that it makes, in order to cut down on system overhead. However, this also makes it even more susceptible to races, especially if the cache or statCache objects are reused between glob calls.
Users are thus advised not to use a glob result as a guarantee of filesystem state in the face of rapid changes. For the vast majority of operations, this is never a problem.
Contributing
Any change to behavior (including bugfixes) must come with a test.
Patches that fail tests or reduce performance will be rejected.