1 [![Build Status](https://travis-ci.org/isaacs/node-glob.svg?branch=master)](https://travis-ci.org/isaacs/node-glob/) [![Dependency Status](https://david-dm.org/isaacs/node-glob.svg)](https://david-dm.org/isaacs/node-glob) [![devDependency Status](https://david-dm.org/isaacs/node-glob/dev-status.svg)](https://david-dm.org/isaacs/node-glob#info=devDependencies) [![optionalDependency Status](https://david-dm.org/isaacs/node-glob/optional-status.svg)](https://david-dm.org/isaacs/node-glob#info=optionalDependencies)
5 Match files using the patterns the shell uses, like stars and stuff.
7 This is a glob implementation in JavaScript. It uses the `minimatch`
8 library to do its matching.
15 var glob = require("glob")
17 // options is optional
18 glob("**/*.js", options, function (er, files) {
19 // files is an array of filenames.
20 // If the `nonull` option is set, and nothing
21 // was found, then files is ["**/*.js"]
22 // er is an error object or null.
28 "Globs" are the patterns you type when you do stuff like `ls *.js` on
29 the command line, or put `build/*` in a `.gitignore` file.
31 Before parsing the path part patterns, braced sections are expanded
32 into a set. Braced sections start with `{` and end with `}`, with any
33 number of comma-delimited sections within. Braced sections may contain
34 slash characters, so `a{/b/c,bcd}` would expand into `a/b/c` and `abcd`.
36 The following characters have special magic meaning when used in a
39 * `*` Matches 0 or more characters in a single path portion
40 * `?` Matches 1 character
41 * `[...]` Matches a range of characters, similar to a RegExp range.
42 If the first character of the range is `!` or `^` then it matches
43 any character not in the range.
44 * `!(pattern|pattern|pattern)` Matches anything that does not match
45 any of the patterns provided.
46 * `?(pattern|pattern|pattern)` Matches zero or one occurrence of the
48 * `+(pattern|pattern|pattern)` Matches one or more occurrences of the
50 * `*(a|b|c)` Matches zero or more occurrences of the patterns provided
51 * `@(pattern|pat*|pat?erN)` Matches exactly one of the patterns
53 * `**` If a "globstar" is alone in a path portion, then it matches
54 zero or more directories and subdirectories searching for matches.
55 It does not crawl symlinked directories.
59 If a file or directory path portion has a `.` as the first character,
60 then it will not match any glob pattern unless that pattern's
61 corresponding path part also has a `.` as its first character.
63 For example, the pattern `a/.*/c` would match the file at `a/.b/c`.
64 However the pattern `a/*/c` would not, because `*` does not start with
67 You can make glob treat dots as normal characters by setting
68 `dot:true` in the options.
72 If you set `matchBase:true` in the options, and the pattern has no
73 slashes in it, then it will seek for any file anywhere in the tree
74 with a matching basename. For example, `*.js` would match
75 `test/simple/basic.js`.
79 The intent for negation would be for a pattern starting with `!` to
80 match everything that *doesn't* match the supplied pattern. However,
81 the implementation is weird, and for the time being, this should be
82 avoided. The behavior will change or be deprecated in version 5.
86 If no matching files are found, then an empty array is returned. This
87 differs from the shell, where the pattern itself is returned. For
93 To get the bash-style behavior, set the `nonull:true` in the options.
98 * `man bash` (Search for "Pattern Matching")
101 * [minimatch documentation](https://github.com/isaacs/minimatch)
103 ## glob.hasMagic(pattern, [options])
105 Returns `true` if there are any special characters in the pattern, and
108 Note that the options affect the results. If `noext:true` is set in
109 the options object, then `+(a|b)` will not be considered a magic
110 pattern. If the pattern has a brace expansion, like `a/{b/c,x/y}`
111 then that is considered magical, unless `nobrace:true` is set in the
114 ## glob(pattern, [options], cb)
116 * `pattern` {String} Pattern to be matched
119 * `err` {Error | null}
120 * `matches` {Array<String>} filenames found matching the pattern
122 Perform an asynchronous glob search.
124 ## glob.sync(pattern, [options])
126 * `pattern` {String} Pattern to be matched
128 * return: {Array<String>} filenames found matching the pattern
130 Perform a synchronous glob search.
134 Create a Glob object by instantiating the `glob.Glob` class.
137 var Glob = require("glob").Glob
138 var mg = new Glob(pattern, options, cb)
141 It's an EventEmitter, and starts walking the filesystem to find matches
144 ### new glob.Glob(pattern, [options], [cb])
146 * `pattern` {String} pattern to search for
148 * `cb` {Function} Called when an error occurs, or matches are found
149 * `err` {Error | null}
150 * `matches` {Array<String>} filenames found matching the pattern
152 Note that if the `sync` flag is set in the options, then matches will
153 be immediately available on the `g.found` member.
157 * `minimatch` The minimatch object that the glob uses.
158 * `options` The options object passed in.
159 * `aborted` Boolean which is set to true when calling `abort()`. There
160 is no way at this time to continue a glob search after aborting, but
161 you can re-use the statCache to avoid having to duplicate syscalls.
162 * `statCache` Collection of all the stat results the glob search
164 * `cache` Convenience object. Each field has the following possible
166 * `false` - Path does not exist
167 * `true` - Path exists
168 * `'DIR'` - Path exists, and is not a directory
169 * `'FILE'` - Path exists, and is a directory
170 * `[file, entries, ...]` - Path exists, is a directory, and the
171 array value is the results of `fs.readdir`
172 * `statCache` Cache of `fs.stat` results, to prevent statting the same
174 * `symlinks` A record of which paths are symbolic links, which is
175 relevant in resolving `**` patterns.
176 * `realpathCache` An optional object which is passed to `fs.realpath`
177 to minimize unnecessary syscalls. It is stored on the instantiated
178 Glob object, and may be re-used.
182 * `end` When the matching is finished, this is emitted with all the
183 matches found. If the `nonull` option is set, and no match was found,
184 then the `matches` list contains the original pattern. The matches
185 are sorted, unless the `nosort` flag is set.
186 * `match` Every time a match is found, this is emitted with the matched.
187 * `error` Emitted when an unexpected error is encountered, or whenever
188 any fs error occurs if `options.strict` is set.
189 * `abort` When `abort()` is called, this event is raised.
193 * `pause` Temporarily stop the search
194 * `resume` Resume the search
195 * `abort` Stop the search forever
199 All the options that can be passed to Minimatch can also be passed to
200 Glob to change pattern matching behavior. Also, some have been added,
201 or have glob-specific ramifications.
203 All options are false by default, unless otherwise noted.
205 All options are added to the Glob object, as well.
207 If you are running many `glob` operations, you can pass a Glob object
208 as the `options` argument to a subsequent operation to shortcut some
209 `stat` and `readdir` calls. At the very least, you may pass in shared
210 `symlinks`, `statCache`, `realpathCache`, and `cache` options, so that
211 parallel glob operations will be sped up by sharing information about
214 * `cwd` The current working directory in which to search. Defaults
216 * `root` The place where patterns starting with `/` will be mounted
217 onto. Defaults to `path.resolve(options.cwd, "/")` (`/` on Unix
218 systems, and `C:\` or some such on Windows.)
219 * `dot` Include `.dot` files in normal matches and `globstar` matches.
220 Note that an explicit dot in a portion of the pattern will always
222 * `nomount` By default, a pattern starting with a forward-slash will be
223 "mounted" onto the root setting, so that a valid filesystem path is
224 returned. Set this flag to disable that behavior.
225 * `mark` Add a `/` character to directory matches. Note that this
226 requires additional stat calls.
227 * `nosort` Don't sort the results.
228 * `stat` Set to true to stat *all* results. This reduces performance
229 somewhat, and is completely unnecessary, unless `readdir` is presumed
230 to be an untrustworthy indicator of file existence.
231 * `silent` When an unusual error is encountered when attempting to
232 read a directory, a warning will be printed to stderr. Set the
233 `silent` option to true to suppress these warnings.
234 * `strict` When an unusual error is encountered when attempting to
235 read a directory, the process will just continue on in search of
236 other matches. Set the `strict` option to raise an error in these
238 * `cache` See `cache` property above. Pass in a previously generated
239 cache object to save some fs calls.
240 * `statCache` A cache of results of filesystem information, to prevent
241 unnecessary stat calls. While it should not normally be necessary
242 to set this, you may pass the statCache from one glob() call to the
243 options object of another, if you know that the filesystem will not
244 change between calls. (See "Race Conditions" below.)
245 * `symlinks` A cache of known symbolic links. You may pass in a
246 previously generated `symlinks` object to save `lstat` calls when
247 resolving `**` matches.
248 * `sync` DEPRECATED: use `glob.sync(pattern, opts)` instead.
249 * `nounique` In some cases, brace-expanded patterns can result in the
250 same file showing up multiple times in the result set. By default,
251 this implementation prevents duplicates in the result set. Set this
252 flag to disable that behavior.
253 * `nonull` Set to never return an empty set, instead returning a set
254 containing the pattern itself. This is the default in glob(3).
255 * `debug` Set to enable debug logging in minimatch and glob.
256 * `nobrace` Do not expand `{a,b}` and `{1..3}` brace sets.
257 * `noglobstar` Do not match `**` against multiple filenames. (Ie,
258 treat it as a normal `*` instead.)
259 * `noext` Do not match `+(a|b)` "extglob" patterns.
260 * `nocase` Perform a case-insensitive match. Note: on
261 case-insensitive filesystems, non-magic patterns will match by
262 default, since `stat` and `readdir` will not raise errors.
263 * `matchBase` Perform a basename-only match if the pattern does not
264 contain any slash characters. That is, `*.js` would be treated as
265 equivalent to `**/*.js`, matching all js files in all directories.
266 * `nonegate` Suppress `negate` behavior. (See below.)
267 * `nocomment` Suppress `comment` behavior. (See below.)
268 * `nonull` Return the pattern when no matches are found.
269 * `nodir` Do not match directories, only files. (Note: to match
270 *only* directories, simply put a `/` at the end of the pattern.)
271 * `ignore` Add a pattern or an array of patterns to exclude matches.
272 * `follow` Follow symlinked directories when expanding `**` patterns.
273 Note that this can result in a lot of duplicate references in the
274 presence of cyclic links.
275 * `realpath` Set to true to call `fs.realpath` on all of the results.
276 In the case of a symlink that cannot be resolved, the full absolute
277 path to the matched entry is returned (though it will usually be a
280 ## Comparisons to other fnmatch/glob implementations
282 While strict compliance with the existing standards is a worthwhile
283 goal, some discrepancies exist between node-glob and other
284 implementations, and are intentional.
286 If the pattern starts with a `!` character, then it is negated. Set the
287 `nonegate` flag to suppress this behavior, and treat leading `!`
288 characters normally. This is perhaps relevant if you wish to start the
289 pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
290 characters at the start of a pattern will negate the pattern multiple
293 If a pattern starts with `#`, then it is treated as a comment, and
294 will not match anything. Use `\#` to match a literal `#` at the
295 start of a line, or set the `nocomment` flag to suppress this behavior.
297 The double-star character `**` is supported by default, unless the
298 `noglobstar` flag is set. This is supported in the manner of bsdglob
299 and bash 4.3, where `**` only has special significance if it is the only
300 thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
303 Note that symlinked directories are not crawled as part of a `**`,
304 though their contents may match against subsequent portions of the
305 pattern. This prevents infinite loops and duplicates and the like.
307 If an escaped pattern has no matches, and the `nonull` flag is set,
308 then glob returns the pattern as-provided, rather than
309 interpreting the character escapes. For example,
310 `glob.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
311 `"*a?"`. This is akin to setting the `nullglob` option in bash, except
312 that it does not resolve escaped pattern characters.
314 If brace expansion is not disabled, then it is performed before any
315 other interpretation of the glob pattern. Thus, a pattern like
316 `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
317 **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
318 checked for validity. Since those two are valid, matching proceeds.
322 **Please only use forward-slashes in glob expressions.**
324 Though windows uses either `/` or `\` as its path separator, only `/`
325 characters are used by this glob implementation. You must use
326 forward-slashes **only** in glob expressions. Back-slashes will always
327 be interpreted as escape characters, not path separators.
329 Results from absolute patterns such as `/foo/*` are mounted onto the
330 root setting using `path.join`. On windows, this will by default result
331 in `/foo/*` matching `C:\foo\bar.txt`.
335 Glob searching, by its very nature, is susceptible to race conditions,
336 since it relies on directory walking and such.
338 As a result, it is possible that a file that exists when glob looks for
339 it may have been deleted or modified by the time it returns the result.
341 As part of its internal implementation, this program caches all stat
342 and readdir calls that it makes, in order to cut down on system
343 overhead. However, this also makes it even more susceptible to races,
344 especially if the cache or statCache objects are reused between glob
347 Users are thus advised not to use a glob result as a guarantee of
348 filesystem state in the face of rapid changes. For the vast majority
349 of operations, this is never a problem.
353 Any change to behavior (including bugfixes) must come with a test.
355 Patches that fail tests or reduce performance will be rejected.
361 # to re-generate test fixtures
364 # to benchmark against bash/zsh
367 # to profile javascript