UNPKG

46.6 kBMarkdownView Raw
1# Glob
2
3Match files using the patterns the shell uses.
4
5The most correct and second fastest glob implementation in
6JavaScript. (See **Comparison to Other JavaScript Glob
7Implementations** at the bottom of this readme.)
8
9![a fun cartoon logo made of glob characters](https://github.com/isaacs/node-glob/raw/main/logo/glob.png)
10
11## Usage
12
13Install with npm
14
15```
16npm i glob
17```
18
19**Note** the npm package name is _not_ `node-glob` that's a
20different thing that was abandoned years ago. Just `glob`.
21
22```js
23// load using import
24import { glob, globSync, globStream, globStreamSync, Glob } from 'glob'
25// or using commonjs, that's fine, too
26const {
27 glob,
28 globSync,
29 globStream,
30 globStreamSync,
31 Glob,
32} = require('glob')
33
34// the main glob() and globSync() resolve/return array of filenames
35
36// all js files, but don't look in node_modules
37const jsfiles = await glob('**/*.js', { ignore: 'node_modules/**' })
38
39// pass in a signal to cancel the glob walk
40const stopAfter100ms = await glob('**/*.css', {
41 signal: AbortSignal.timeout(100),
42})
43
44// multiple patterns supported as well
45const images = await glob(['css/*.{png,jpeg}', 'public/*.{png,jpeg}'])
46
47// but of course you can do that with the glob pattern also
48// the sync function is the same, just returns a string[] instead
49// of Promise<string[]>
50const imagesAlt = globSync('{css,public}/*.{png,jpeg}')
51
52// you can also stream them, this is a Minipass stream
53const filesStream = globStream(['**/*.dat', 'logs/**/*.log'])
54
55// construct a Glob object if you wanna do it that way, which
56// allows for much faster walks if you have to look in the same
57// folder multiple times.
58const g = new Glob('**/foo', {})
59// glob objects are async iterators, can also do globIterate() or
60// g.iterate(), same deal
61for await (const file of g) {
62 console.log('found a foo file:', file)
63}
64// pass a glob as the glob options to reuse its settings and caches
65const g2 = new Glob('**/bar', g)
66// sync iteration works as well
67for (const file of g2) {
68 console.log('found a bar file:', file)
69}
70
71// you can also pass withFileTypes: true to get Path objects
72// these are like a Dirent, but with some more added powers
73// check out http://npm.im/path-scurry for more info on their API
74const g3 = new Glob('**/baz/**', { withFileTypes: true })
75g3.stream().on('data', path => {
76 console.log(
77 'got a path object',
78 path.fullpath(),
79 path.isDirectory(),
80 path.readdirSync().map(e => e.name)
81 )
82})
83
84// if you use stat:true and withFileTypes, you can sort results
85// by things like modified time, filter by permission mode, etc.
86// All Stats fields will be available in that case. Slightly
87// slower, though.
88// For example:
89const results = await glob('**', { stat: true, withFileTypes: true })
90
91const timeSortedFiles = results
92 .sort((a, b) => a.mtimeMs - b.mtimeMs)
93 .map(path => path.fullpath())
94
95const groupReadableFiles = results
96 .filter(path => path.mode & 0o040)
97 .map(path => path.fullpath())
98
99// custom ignores can be done like this, for example by saying
100// you'll ignore all markdown files, and all folders named 'docs'
101const customIgnoreResults = await glob('**', {
102 ignore: {
103 ignored: p => /\.md$/.test(p.name),
104 childrenIgnored: p => p.isNamed('docs'),
105 },
106})
107
108// another fun use case, only return files with the same name as
109// their parent folder, plus either `.ts` or `.js`
110const folderNamedModules = await glob('**/*.{ts,js}', {
111 ignore: {
112 ignored: p => {
113 const pp = p.parent
114 return !(p.isNamed(pp.name + '.ts') || p.isNamed(pp.name + '.js'))
115 },
116 },
117})
118
119// find all files edited in the last hour, to do this, we ignore
120// all of them that are more than an hour old
121const newFiles = await glob('**', {
122 // need stat so we have mtime
123 stat: true,
124 // only want the files, not the dirs
125 nodir: true,
126 ignore: {
127 ignored: p => {
128 return new Date() - p.mtime > 60 * 60 * 1000
129 },
130 // could add similar childrenIgnored here as well, but
131 // directory mtime is inconsistent across platforms, so
132 // probably better not to, unless you know the system
133 // tracks this reliably.
134 },
135})
136```
137
138**Note** Glob patterns should always use `/` as a path separator,
139even on Windows systems, as `\` is used to escape glob
140characters. If you wish to use `\` as a path separator _instead
141of_ using it as an escape character on Windows platforms, you may
142set `windowsPathsNoEscape:true` in the options. In this mode,
143special glob characters cannot be escaped, making it impossible
144to match a literal `*` `?` and so on in filenames.
145
146## Command Line Interface
147
148```
149$ glob -h
150
151Usage:
152 glob [options] [<pattern> [<pattern> ...]]
153
154Expand the positional glob expression arguments into any matching file system
155paths found.
156
157 -c<command> --cmd=<command>
158 Run the command provided, passing the glob expression
159 matches as arguments.
160
161 -A --all By default, the glob cli command will not expand any
162 arguments that are an exact match to a file on disk.
163
164 This prevents double-expanding, in case the shell
165 expands an argument whose filename is a glob
166 expression.
167
168 For example, if 'app/*.ts' would match 'app/[id].ts',
169 then on Windows powershell or cmd.exe, 'glob app/*.ts'
170 will expand to 'app/[id].ts', as expected. However, in
171 posix shells such as bash or zsh, the shell will first
172 expand 'app/*.ts' to a list of filenames. Then glob
173 will look for a file matching 'app/[id].ts' (ie,
174 'app/i.ts' or 'app/d.ts'), which is unexpected.
175
176 Setting '--all' prevents this behavior, causing glob to
177 treat ALL patterns as glob expressions to be expanded,
178 even if they are an exact match to a file on disk.
179
180 When setting this option, be sure to enquote arguments
181 so that the shell will not expand them prior to passing
182 them to the glob command process.
183
184 -a --absolute Expand to absolute paths
185 -d --dot-relative Prepend './' on relative matches
186 -m --mark Append a / on any directories matched
187 -x --posix Always resolve to posix style paths, using '/' as the
188 directory separator, even on Windows. Drive letter
189 absolute matches on Windows will be expanded to their
190 full resolved UNC maths, eg instead of 'C:\foo\bar', it
191 will expand to '//?/C:/foo/bar'.
192
193 -f --follow Follow symlinked directories when expanding '**'
194 -R --realpath Call 'fs.realpath' on all of the results. In the case
195 of an entry that cannot be resolved, the entry is
196 omitted. This incurs a slight performance penalty, of
197 course, because of the added system calls.
198
199 -s --stat Call 'fs.lstat' on all entries, whether required or not
200 to determine if it's a valid match.
201
202 -b --match-base Perform a basename-only match if the pattern does not
203 contain any slash characters. That is, '*.js' would be
204 treated as equivalent to '**/*.js', matching js files
205 in all directories.
206
207 --dot Allow patterns to match files/directories that start
208 with '.', even if the pattern does not start with '.'
209
210 --nobrace Do not expand {...} patterns
211 --nocase Perform a case-insensitive match. This defaults to
212 'true' on macOS and Windows platforms, and false on all
213 others.
214
215 Note: 'nocase' should only be explicitly set when it is
216 known that the filesystem's case sensitivity differs
217 from the platform default. If set 'true' on
218 case-insensitive file systems, then the walk may return
219 more or less results than expected.
220
221 --nodir Do not match directories, only files.
222
223 Note: to *only* match directories, append a '/' at the
224 end of the pattern.
225
226 --noext Do not expand extglob patterns, such as '+(a|b)'
227 --noglobstar Do not expand '**' against multiple path portions. Ie,
228 treat it as a normal '*' instead.
229
230 --windows-path-no-escape
231 Use '\' as a path separator *only*, and *never* as an
232 escape character. If set, all '\' characters are
233 replaced with '/' in the pattern.
234
235 -D<n> --max-depth=<n> Maximum depth to traverse from the current working
236 directory
237
238 -C<cwd> --cwd=<cwd> Current working directory to execute/match in
239 -r<root> --root=<root> A string path resolved against the 'cwd', which is used
240 as the starting point for absolute patterns that start
241 with '/' (but not drive letters or UNC paths on
242 Windows).
243
244 Note that this *doesn't* necessarily limit the walk to
245 the 'root' directory, and doesn't affect the cwd
246 starting point for non-absolute patterns. A pattern
247 containing '..' will still be able to traverse out of
248 the root directory, if it is not an actual root
249 directory on the filesystem, and any non-absolute
250 patterns will still be matched in the 'cwd'.
251
252 To start absolute and non-absolute patterns in the same
253 path, you can use '--root=' to set it to the empty
254 string. However, be aware that on Windows systems, a
255 pattern like 'x:/*' or '//host/share/*' will *always*
256 start in the 'x:/' or '//host/share/' directory,
257 regardless of the --root setting.
258
259 --platform=<platform> Defaults to the value of 'process.platform' if
260 available, or 'linux' if not. Setting --platform=win32
261 on non-Windows systems may cause strange behavior!
262
263 -i<ignore> --ignore=<ignore>
264 Glob patterns to ignore Can be set multiple times
265 -v --debug Output a huge amount of noisy debug information about
266 patterns as they are parsed and used to match files.
267
268 -h --help Show this usage information
269```
270
271## `glob(pattern: string | string[], options?: GlobOptions) => Promise<string[] | Path[]>`
272
273Perform an asynchronous glob search for the pattern(s) specified.
274Returns
275[Path](https://isaacs.github.io/path-scurry/classes/PathBase)
276objects if the `withFileTypes` option is set to `true`. See below
277for full options field desciptions.
278
279## `globSync(pattern: string | string[], options?: GlobOptions) => string[] | Path[]`
280
281Synchronous form of `glob()`.
282
283Alias: `glob.sync()`
284
285## `globIterate(pattern: string | string[], options?: GlobOptions) => AsyncGenerator<string>`
286
287Return an async iterator for walking glob pattern matches.
288
289Alias: `glob.iterate()`
290
291## `globIterateSync(pattern: string | string[], options?: GlobOptions) => Generator<string>`
292
293Return a sync iterator for walking glob pattern matches.
294
295Alias: `glob.iterate.sync()`, `glob.sync.iterate()`
296
297## `globStream(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>`
298
299Return a stream that emits all the strings or `Path` objects and
300then emits `end` when completed.
301
302Alias: `glob.stream()`
303
304## `globStreamSync(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>`
305
306Syncronous form of `globStream()`. Will read all the matches as
307fast as you consume them, even all in a single tick if you
308consume them immediately, but will still respond to backpressure
309if they're not consumed immediately.
310
311Alias: `glob.stream.sync()`, `glob.sync.stream()`
312
313## `hasMagic(pattern: string | string[], options?: GlobOptions) => boolean`
314
315Returns `true` if the provided pattern contains any "magic" glob
316characters, given the options provided.
317
318Brace expansion is not considered "magic" unless the
319`magicalBraces` option is set, as brace expansion just turns one
320string into an array of strings. So a pattern like `'x{a,b}y'`
321would return `false`, because `'xay'` and `'xby'` both do not
322contain any magic glob characters, and it's treated the same as
323if you had called it on `['xay', 'xby']`. When
324`magicalBraces:true` is in the options, brace expansion _is_
325treated as a pattern having magic.
326
327## `escape(pattern: string, options?: GlobOptions) => string`
328
329Escape all magic characters in a glob pattern, so that it will
330only ever match literal strings
331
332If the `windowsPathsNoEscape` option is used, then characters are
333escaped by wrapping in `[]`, because a magic character wrapped in
334a character class can only be satisfied by that exact character.
335
336Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
337be escaped or unescaped.
338
339## `unescape(pattern: string, options?: GlobOptions) => string`
340
341Un-escape a glob string that may contain some escaped characters.
342
343If the `windowsPathsNoEscape` option is used, then square-brace
344escapes are removed, but not backslash escapes. For example, it
345will turn the string `'[*]'` into `*`, but it will not turn
346`'\\*'` into `'*'`, because `\` is a path separator in
347`windowsPathsNoEscape` mode.
348
349When `windowsPathsNoEscape` is not set, then both brace escapes
350and backslash escapes are removed.
351
352Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
353be escaped or unescaped.
354
355## Class `Glob`
356
357An object that can perform glob pattern traversals.
358
359### `const g = new Glob(pattern: string | string[], options: GlobOptions)`
360
361Options object is required.
362
363See full options descriptions below.
364
365Note that a previous `Glob` object can be passed as the
366`GlobOptions` to another `Glob` instantiation to re-use settings
367and caches with a new pattern.
368
369Traversal functions can be called multiple times to run the walk
370again.
371
372### `g.stream()`
373
374Stream results asynchronously,
375
376### `g.streamSync()`
377
378Stream results synchronously.
379
380### `g.iterate()`
381
382Default async iteration function. Returns an AsyncGenerator that
383iterates over the results.
384
385### `g.iterateSync()`
386
387Default sync iteration function. Returns a Generator that
388iterates over the results.
389
390### `g.walk()`
391
392Returns a Promise that resolves to the results array.
393
394### `g.walkSync()`
395
396Returns a results array.
397
398### Properties
399
400All options are stored as properties on the `Glob` object.
401
402- `opts` The options provided to the constructor.
403- `patterns` An array of parsed immutable `Pattern` objects.
404
405## Options
406
407Exported as `GlobOptions` TypeScript interface. A `GlobOptions`
408object may be provided to any of the exported methods, and must
409be provided to the `Glob` constructor.
410
411All options are optional, boolean, and false by default, unless
412otherwise noted.
413
414All resolved options are added to the Glob object as properties.
415
416If you are running many `glob` operations, you can pass a Glob
417object as the `options` argument to a subsequent operation to
418share the previously loaded cache.
419
420- `cwd` String path or `file://` string or URL object. The
421 current working directory in which to search. Defaults to
422 `process.cwd()`. See also: "Windows, CWDs, Drive Letters, and
423 UNC Paths", below.
424
425 This option may be either a string path or a `file://` URL
426 object or string.
427
428- `root` A string path resolved against the `cwd` option, which
429 is used as the starting point for absolute patterns that start
430 with `/`, (but not drive letters or UNC paths on Windows).
431
432 Note that this _doesn't_ necessarily limit the walk to the
433 `root` directory, and doesn't affect the cwd starting point for
434 non-absolute patterns. A pattern containing `..` will still be
435 able to traverse out of the root directory, if it is not an
436 actual root directory on the filesystem, and any non-absolute
437 patterns will be matched in the `cwd`. For example, the
438 pattern `/../*` with `{root:'/some/path'}` will return all
439 files in `/some`, not all files in `/some/path`. The pattern
440 `*` with `{root:'/some/path'}` will return all the entries in
441 the cwd, not the entries in `/some/path`.
442
443 To start absolute and non-absolute patterns in the same
444 path, you can use `{root:''}`. However, be aware that on
445 Windows systems, a pattern like `x:/*` or `//host/share/*` will
446 _always_ start in the `x:/` or `//host/share` directory,
447 regardless of the `root` setting.
448
449- `windowsPathsNoEscape` Use `\\` as a path separator _only_, and
450 _never_ as an escape character. If set, all `\\` characters are
451 replaced with `/` in the pattern.
452
453 Note that this makes it **impossible** to match against paths
454 containing literal glob pattern characters, but allows matching
455 with patterns constructed using `path.join()` and
456 `path.resolve()` on Windows platforms, mimicking the (buggy!)
457 behavior of Glob v7 and before on Windows. Please use with
458 caution, and be mindful of [the caveat below about Windows
459 paths](#windows). (For legacy reasons, this is also set if
460 `allowWindowsEscape` is set to the exact value `false`.)
461
462- `dot` Include `.dot` files in normal matches and `globstar`
463 matches. Note that an explicit dot in a portion of the pattern
464 will always match dot files.
465
466- `magicalBraces` Treat brace expansion like `{a,b}` as a "magic"
467 pattern. Has no effect if {@link nobrace} is set.
468
469 Only has effect on the {@link hasMagic} function, no effect on
470 glob pattern matching itself.
471
472- `dotRelative` Prepend all relative path strings with `./` (or
473 `.\` on Windows).
474
475 Without this option, returned relative paths are "bare", so
476 instead of returning `'./foo/bar'`, they are returned as
477 `'foo/bar'`.
478
479 Relative patterns starting with `'../'` are not prepended with
480 `./`, even if this option is set.
481
482- `mark` Add a `/` character to directory matches. Note that this
483 requires additional stat calls.
484
485- `nobrace` Do not expand `{a,b}` and `{1..3}` brace sets.
486
487- `noglobstar` Do not match `**` against multiple filenames. (Ie,
488 treat it as a normal `*` instead.)
489
490- `noext` Do not match "extglob" patterns such as `+(a|b)`.
491
492- `nocase` Perform a case-insensitive match. This defaults to
493 `true` on macOS and Windows systems, and `false` on all others.
494
495 **Note** `nocase` should only be explicitly set when it is
496 known that the filesystem's case sensitivity differs from the
497 platform default. If set `true` on case-sensitive file
498 systems, or `false` on case-insensitive file systems, then the
499 walk may return more or less results than expected.
500
501- `maxDepth` Specify a number to limit the depth of the directory
502 traversal to this many levels below the `cwd`.
503
504- `matchBase` Perform a basename-only match if the pattern does
505 not contain any slash characters. That is, `*.js` would be
506 treated as equivalent to `**/*.js`, matching all js files in
507 all directories.
508
509- `nodir` Do not match directories, only files. (Note: to match
510 _only_ directories, put a `/` at the end of the pattern.)
511
512 Note: when `follow` and `nodir` are both set, then symbolic
513 links to directories are also omitted.
514
515- `stat` Call `lstat()` on all entries, whether required or not
516 to determine whether it's a valid match. When used with
517 `withFileTypes`, this means that matches will include data such
518 as modified time, permissions, and so on. Note that this will
519 incur a performance cost due to the added system calls.
520
521- `ignore` string or string[], or an object with `ignore` and
522 `ignoreChildren` methods.
523
524 If a string or string[] is provided, then this is treated as a
525 glob pattern or array of glob patterns to exclude from matches.
526 To ignore all children within a directory, as well as the entry
527 itself, append `'/**'` to the ignore pattern.
528
529 **Note** `ignore` patterns are _always_ in `dot:true` mode,
530 regardless of any other settings.
531
532 If an object is provided that has `ignored(path)` and/or
533 `childrenIgnored(path)` methods, then these methods will be
534 called to determine whether any Path is a match or if its
535 children should be traversed, respectively.
536
537- `follow` Follow symlinked directories when expanding `**`
538 patterns. This can result in a lot of duplicate references in
539 the presence of cyclic links, and make performance quite bad.
540
541 By default, a `**` in a pattern will follow 1 symbolic link if
542 it is not the first item in the pattern, or none if it is the
543 first item in the pattern, following the same behavior as Bash.
544
545 Note: when `follow` and `nodir` are both set, then symbolic
546 links to directories are also omitted.
547
548- `realpath` Set to true to call `fs.realpath` on all of the
549 results. In the case of an entry that cannot be resolved, the
550 entry is omitted. This incurs a slight performance penalty, of
551 course, because of the added system calls.
552
553- `absolute` Set to true to always receive absolute paths for
554 matched files. Set to `false` to always receive relative paths
555 for matched files.
556
557 By default, when this option is not set, absolute paths are
558 returned for patterns that are absolute, and otherwise paths
559 are returned that are relative to the `cwd` setting.
560
561 This does _not_ make an extra system call to get the realpath,
562 it only does string path resolution.
563
564 `absolute` may not be used along with `withFileTypes`.
565
566- `posix` Set to true to use `/` as the path separator in
567 returned results. On posix systems, this has no effect. On
568 Windows systems, this will return `/` delimited path results,
569 and absolute paths will be returned in their full resolved UNC
570 path form, eg insted of `'C:\\foo\\bar'`, it will return
571 `//?/C:/foo/bar`.
572
573- `platform` Defaults to value of `process.platform` if
574 available, or `'linux'` if not. Setting `platform:'win32'` on
575 non-Windows systems may cause strange behavior.
576
577- `withFileTypes` Return [PathScurry](http://npm.im/path-scurry)
578 `Path` objects instead of strings. These are similar to a
579 NodeJS `Dirent` object, but with additional methods and
580 properties.
581
582 `withFileTypes` may not be used along with `absolute`.
583
584- `signal` An AbortSignal which will cancel the Glob walk when
585 triggered.
586
587- `fs` An override object to pass in custom filesystem methods.
588 See [PathScurry docs](http://npm.im/path-scurry) for what can
589 be overridden.
590
591- `scurry` A [PathScurry](http://npm.im/path-scurry) object used
592 to traverse the file system. If the `nocase` option is set
593 explicitly, then any provided `scurry` object must match this
594 setting.
595
596## Glob Primer
597
598Much more information about glob pattern expansion can be found
599by running `man bash` and searching for `Pattern Matching`.
600
601"Globs" are the patterns you type when you do stuff like `ls
602*.js` on the command line, or put `build/*` in a `.gitignore`
603file.
604
605Before parsing the path part patterns, braced sections are
606expanded into a set. Braced sections start with `{` and end with
607`}`, with 2 or more comma-delimited sections within. Braced
608sections may contain slash characters, so `a{/b/c,bcd}` would
609expand into `a/b/c` and `abcd`.
610
611The following characters have special magic meaning when used in
612a path portion. With the exception of `**`, none of these match
613path separators (ie, `/` on all platforms, and `\` on Windows).
614
615- `*` Matches 0 or more characters in a single path portion.
616 When alone in a path portion, it must match at least 1
617 character. If `dot:true` is not specified, then `*` will not
618 match against a `.` character at the start of a path portion.
619- `?` Matches 1 character. If `dot:true` is not specified, then
620 `?` will not match against a `.` character at the start of a
621 path portion.
622- `[...]` Matches a range of characters, similar to a RegExp
623 range. If the first character of the range is `!` or `^` then
624 it matches any character not in the range. If the first
625 character is `]`, then it will be considered the same as `\]`,
626 rather than the end of the character class.
627- `!(pattern|pattern|pattern)` Matches anything that does not
628 match any of the patterns provided. May _not_ contain `/`
629 characters. Similar to `*`, if alone in a path portion, then
630 the path portion must have at least one character.
631- `?(pattern|pattern|pattern)` Matches zero or one occurrence of
632 the patterns provided. May _not_ contain `/` characters.
633- `+(pattern|pattern|pattern)` Matches one or more occurrences of
634 the patterns provided. May _not_ contain `/` characters.
635- `*(a|b|c)` Matches zero or more occurrences of the patterns
636 provided. May _not_ contain `/` characters.
637- `@(pattern|pat*|pat?erN)` Matches exactly one of the patterns
638 provided. May _not_ contain `/` characters.
639- `**` If a "globstar" is alone in a path portion, then it
640 matches zero or more directories and subdirectories searching
641 for matches. It does not crawl symlinked directories, unless
642 `{follow:true}` is passed in the options object. A pattern
643 like `a/b/**` will only match `a/b` if it is a directory.
644 Follows 1 symbolic link if not the first item in the pattern,
645 or 0 if it is the first item, unless `follow:true` is set, in
646 which case it follows all symbolic links.
647
648`[:class:]` patterns are supported by this implementation, but
649`[=c=]` and `[.symbol.]` style class patterns are not.
650
651### Dots
652
653If a file or directory path portion has a `.` as the first
654character, then it will not match any glob pattern unless that
655pattern's corresponding path part also has a `.` as its first
656character.
657
658For example, the pattern `a/.*/c` would match the file at
659`a/.b/c`. However the pattern `a/*/c` would not, because `*` does
660not start with a dot character.
661
662You can make glob treat dots as normal characters by setting
663`dot:true` in the options.
664
665### Basename Matching
666
667If you set `matchBase:true` in the options, and the pattern has
668no slashes in it, then it will seek for any file anywhere in the
669tree with a matching basename. For example, `*.js` would match
670`test/simple/basic.js`.
671
672### Empty Sets
673
674If no matching files are found, then an empty array is returned.
675This differs from the shell, where the pattern itself is
676returned. For example:
677
678```sh
679$ echo a*s*d*f
680a*s*d*f
681```
682
683## Comparisons to other fnmatch/glob implementations
684
685While strict compliance with the existing standards is a
686worthwhile goal, some discrepancies exist between node-glob and
687other implementations, and are intentional.
688
689The double-star character `**` is supported by default, unless
690the `noglobstar` flag is set. This is supported in the manner of
691bsdglob and bash 5, where `**` only has special significance if
692it is the only thing in a path part. That is, `a/**/b` will match
693`a/x/y/b`, but `a/**b` will not.
694
695Note that symlinked directories are not traversed as part of a
696`**`, though their contents may match against subsequent portions
697of the pattern. This prevents infinite loops and duplicates and
698the like. You can force glob to traverse symlinks with `**` by
699setting `{follow:true}` in the options.
700
701There is no equivalent of the `nonull` option. A pattern that
702does not find any matches simply resolves to nothing. (An empty
703array, immediately ended stream, etc.)
704
705If brace expansion is not disabled, then it is performed before
706any other interpretation of the glob pattern. Thus, a pattern
707like `+(a|{b),c)}`, which would not be valid in bash or zsh, is
708expanded **first** into the set of `+(a|b)` and `+(a|c)`, and
709those patterns are checked for validity. Since those two are
710valid, matching proceeds.
711
712The character class patterns `[:class:]` (posix standard named
713classes) style class patterns are supported and unicode-aware,
714but `[=c=]` (locale-specific character collation weight), and
715`[.symbol.]` (collating symbol), are not.
716
717### Repeated Slashes
718
719Unlike Bash and zsh, repeated `/` are always coalesced into a
720single path separator.
721
722### Comments and Negation
723
724Previously, this module let you mark a pattern as a "comment" if
725it started with a `#` character, or a "negated" pattern if it
726started with a `!` character.
727
728These options were deprecated in version 5, and removed in
729version 6.
730
731To specify things that should not match, use the `ignore` option.
732
733## Windows
734
735**Please only use forward-slashes in glob expressions.**
736
737Though windows uses either `/` or `\` as its path separator, only
738`/` characters are used by this glob implementation. You must use
739forward-slashes **only** in glob expressions. Back-slashes will
740always be interpreted as escape characters, not path separators.
741
742Results from absolute patterns such as `/foo/*` are mounted onto
743the root setting using `path.join`. On windows, this will by
744default result in `/foo/*` matching `C:\foo\bar.txt`.
745
746To automatically coerce all `\` characters to `/` in pattern
747strings, **thus making it impossible to escape literal glob
748characters**, you may set the `windowsPathsNoEscape` option to
749`true`.
750
751### Windows, CWDs, Drive Letters, and UNC Paths
752
753On posix systems, when a pattern starts with `/`, any `cwd`
754option is ignored, and the traversal starts at `/`, plus any
755non-magic path portions specified in the pattern.
756
757On Windows systems, the behavior is similar, but the concept of
758an "absolute path" is somewhat more involved.
759
760#### UNC Paths
761
762A UNC path may be used as the start of a pattern on Windows
763platforms. For example, a pattern like: `//?/x:/*` will return
764all file entries in the root of the `x:` drive. A pattern like
765`//ComputerName/Share/*` will return all files in the associated
766share.
767
768UNC path roots are always compared case insensitively.
769
770#### Drive Letters
771
772A pattern starting with a drive letter, like `c:/*`, will search
773in that drive, regardless of any `cwd` option provided.
774
775If the pattern starts with `/`, and is not a UNC path, and there
776is an explicit `cwd` option set with a drive letter, then the
777drive letter in the `cwd` is used as the root of the directory
778traversal.
779
780For example, `glob('/tmp', { cwd: 'c:/any/thing' })` will return
781`['c:/tmp']` as the result.
782
783If an explicit `cwd` option is not provided, and the pattern
784starts with `/`, then the traversal will run on the root of the
785drive provided as the `cwd` option. (That is, it is the result of
786`path.resolve('/')`.)
787
788## Race Conditions
789
790Glob searching, by its very nature, is susceptible to race
791conditions, since it relies on directory walking.
792
793As a result, it is possible that a file that exists when glob
794looks for it may have been deleted or modified by the time it
795returns the result.
796
797By design, this implementation caches all readdir calls that it
798makes, in order to cut down on system overhead. However, this
799also makes it even more susceptible to races, especially if the
800cache object is reused between glob calls.
801
802Users are thus advised not to use a glob result as a guarantee of
803filesystem state in the face of rapid changes. For the vast
804majority of operations, this is never a problem.
805
806### See Also:
807
808- `man sh`
809- `man bash` [Pattern
810 Matching](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html)
811- `man 3 fnmatch`
812- `man 5 gitignore`
813- [minimatch documentation](https://github.com/isaacs/minimatch)
814
815## Glob Logo
816
817Glob's logo was created by [Tanya
818Brassie](http://tanyabrassie.com/). Logo files can be found
819[here](https://github.com/isaacs/node-glob/tree/master/logo).
820
821The logo is licensed under a [Creative Commons
822Attribution-ShareAlike 4.0 International
823License](https://creativecommons.org/licenses/by-sa/4.0/).
824
825## Contributing
826
827Any change to behavior (including bugfixes) must come with a
828test.
829
830Patches that fail tests or reduce performance will be rejected.
831
832```sh
833# to run tests
834npm test
835
836# to re-generate test fixtures
837npm run test-regen
838
839# run the benchmarks
840npm run bench
841
842# to profile javascript
843npm run prof
844```
845
846## Comparison to Other JavaScript Glob Implementations
847
848**tl;dr**
849
850- If you want glob matching that is as faithful as possible to
851 Bash pattern expansion semantics, and as fast as possible
852 within that constraint, _use this module_.
853- If you are reasonably sure that the patterns you will encounter
854 are relatively simple, and want the absolutely fastest glob
855 matcher out there, _use [fast-glob](http://npm.im/fast-glob)_.
856- If you are reasonably sure that the patterns you will encounter
857 are relatively simple, and want the convenience of
858 automatically respecting `.gitignore` files, _use
859 [globby](http://npm.im/globby)_.
860
861There are some other glob matcher libraries on npm, but these
862three are (in my opinion, as of 2023) the best.
863
864---
865
866**full explanation**
867
868Every library reflects a set of opinions and priorities in the
869trade-offs it makes. Other than this library, I can personally
870recommend both [globby](http://npm.im/globby) and
871[fast-glob](http://npm.im/fast-glob), though they differ in their
872benefits and drawbacks.
873
874Both have very nice APIs and are reasonably fast.
875
876`fast-glob` is, as far as I am aware, the fastest glob
877implementation in JavaScript today. However, there are many
878cases where the choices that `fast-glob` makes in pursuit of
879speed mean that its results differ from the results returned by
880Bash and other sh-like shells, which may be surprising.
881
882In my testing, `fast-glob` is around 10-20% faster than this
883module when walking over 200k files nested 4 directories
884deep[1](#fn-webscale). However, there are some inconsistencies
885with Bash matching behavior that this module does not suffer
886from:
887
888- `**` only matches files, not directories
889- `..` path portions are not handled unless they appear at the
890 start of the pattern
891- `./!(<pattern>)` will not match any files that _start_ with
892 `<pattern>`, even if they do not match `<pattern>`. For
893 example, `!(9).txt` will not match `9999.txt`.
894- Some brace patterns in the middle of a pattern will result in
895 failing to find certain matches.
896- Extglob patterns are allowed to contain `/` characters.
897
898Globby exhibits all of the same pattern semantics as fast-glob,
899(as it is a wrapper around fast-glob) and is slightly slower than
900node-glob (by about 10-20% in the benchmark test set, or in other
901words, anywhere from 20-50% slower than fast-glob). However, it
902adds some API conveniences that may be worth the costs.
903
904- Support for `.gitignore` and other ignore files.
905- Support for negated globs (ie, patterns starting with `!`
906 rather than using a separate `ignore` option).
907
908The priority of this module is "correctness" in the sense of
909performing a glob pattern expansion as faithfully as possible to
910the behavior of Bash and other sh-like shells, with as much speed
911as possible.
912
913Note that prior versions of `node-glob` are _not_ on this list.
914Former versions of this module are far too slow for any cases
915where performance matters at all, and were designed with APIs
916that are extremely dated by current JavaScript standards.
917
918---
919
920<small id="fn-webscale">[1]: In the cases where this module
921returns results and `fast-glob` doesn't, it's even faster, of
922course.</small>
923
924![lumpy space princess saying 'oh my GLOB'](https://github.com/isaacs/node-glob/raw/main/oh-my-glob.gif)
925
926### Benchmark Results
927
928First number is time, smaller is better.
929
930Second number is the count of results returned.
931
932```
933--- pattern: '**' ---
934~~ sync ~~
935node fast-glob sync 0m0.598s 200364
936node globby sync 0m0.765s 200364
937node current globSync mjs 0m0.683s 222656
938node current glob syncStream 0m0.649s 222656
939~~ async ~~
940node fast-glob async 0m0.350s 200364
941node globby async 0m0.509s 200364
942node current glob async mjs 0m0.463s 222656
943node current glob stream 0m0.411s 222656
944
945--- pattern: '**/..' ---
946~~ sync ~~
947node fast-glob sync 0m0.486s 0
948node globby sync 0m0.769s 200364
949node current globSync mjs 0m0.564s 2242
950node current glob syncStream 0m0.583s 2242
951~~ async ~~
952node fast-glob async 0m0.283s 0
953node globby async 0m0.512s 200364
954node current glob async mjs 0m0.299s 2242
955node current glob stream 0m0.312s 2242
956
957--- pattern: './**/0/**/0/**/0/**/0/**/*.txt' ---
958~~ sync ~~
959node fast-glob sync 0m0.490s 10
960node globby sync 0m0.517s 10
961node current globSync mjs 0m0.540s 10
962node current glob syncStream 0m0.550s 10
963~~ async ~~
964node fast-glob async 0m0.290s 10
965node globby async 0m0.296s 10
966node current glob async mjs 0m0.278s 10
967node current glob stream 0m0.302s 10
968
969--- pattern: './**/[01]/**/[12]/**/[23]/**/[45]/**/*.txt' ---
970~~ sync ~~
971node fast-glob sync 0m0.500s 160
972node globby sync 0m0.528s 160
973node current globSync mjs 0m0.556s 160
974node current glob syncStream 0m0.573s 160
975~~ async ~~
976node fast-glob async 0m0.283s 160
977node globby async 0m0.301s 160
978node current glob async mjs 0m0.306s 160
979node current glob stream 0m0.322s 160
980
981--- pattern: './**/0/**/0/**/*.txt' ---
982~~ sync ~~
983node fast-glob sync 0m0.502s 5230
984node globby sync 0m0.527s 5230
985node current globSync mjs 0m0.544s 5230
986node current glob syncStream 0m0.557s 5230
987~~ async ~~
988node fast-glob async 0m0.285s 5230
989node globby async 0m0.305s 5230
990node current glob async mjs 0m0.304s 5230
991node current glob stream 0m0.310s 5230
992
993--- pattern: '**/*.txt' ---
994~~ sync ~~
995node fast-glob sync 0m0.580s 200023
996node globby sync 0m0.771s 200023
997node current globSync mjs 0m0.685s 200023
998node current glob syncStream 0m0.649s 200023
999~~ async ~~
1000node fast-glob async 0m0.349s 200023
1001node globby async 0m0.509s 200023
1002node current glob async mjs 0m0.427s 200023
1003node current glob stream 0m0.388s 200023
1004
1005--- pattern: '{**/*.txt,**/?/**/*.txt,**/?/**/?/**/*.txt,**/?/**/?/**/?/**/*.txt,**/?/**/?/**/?/**/?/**/*.txt}' ---
1006~~ sync ~~
1007node fast-glob sync 0m0.589s 200023
1008node globby sync 0m0.771s 200023
1009node current globSync mjs 0m0.716s 200023
1010node current glob syncStream 0m0.684s 200023
1011~~ async ~~
1012node fast-glob async 0m0.351s 200023
1013node globby async 0m0.518s 200023
1014node current glob async mjs 0m0.462s 200023
1015node current glob stream 0m0.468s 200023
1016
1017--- pattern: '**/5555/0000/*.txt' ---
1018~~ sync ~~
1019node fast-glob sync 0m0.496s 1000
1020node globby sync 0m0.519s 1000
1021node current globSync mjs 0m0.539s 1000
1022node current glob syncStream 0m0.567s 1000
1023~~ async ~~
1024node fast-glob async 0m0.285s 1000
1025node globby async 0m0.299s 1000
1026node current glob async mjs 0m0.305s 1000
1027node current glob stream 0m0.301s 1000
1028
1029--- pattern: './**/0/**/../[01]/**/0/../**/0/*.txt' ---
1030~~ sync ~~
1031node fast-glob sync 0m0.484s 0
1032node globby sync 0m0.507s 0
1033node current globSync mjs 0m0.577s 4880
1034node current glob syncStream 0m0.586s 4880
1035~~ async ~~
1036node fast-glob async 0m0.280s 0
1037node globby async 0m0.298s 0
1038node current glob async mjs 0m0.327s 4880
1039node current glob stream 0m0.324s 4880
1040
1041--- pattern: '**/????/????/????/????/*.txt' ---
1042~~ sync ~~
1043node fast-glob sync 0m0.547s 100000
1044node globby sync 0m0.673s 100000
1045node current globSync mjs 0m0.626s 100000
1046node current glob syncStream 0m0.618s 100000
1047~~ async ~~
1048node fast-glob async 0m0.315s 100000
1049node globby async 0m0.414s 100000
1050node current glob async mjs 0m0.366s 100000
1051node current glob stream 0m0.345s 100000
1052
1053--- pattern: './{**/?{/**/?{/**/?{/**/?,,,,},,,,},,,,},,,}/**/*.txt' ---
1054~~ sync ~~
1055node fast-glob sync 0m0.588s 100000
1056node globby sync 0m0.670s 100000
1057node current globSync mjs 0m0.717s 200023
1058node current glob syncStream 0m0.687s 200023
1059~~ async ~~
1060node fast-glob async 0m0.343s 100000
1061node globby async 0m0.418s 100000
1062node current glob async mjs 0m0.519s 200023
1063node current glob stream 0m0.451s 200023
1064
1065--- pattern: '**/!(0|9).txt' ---
1066~~ sync ~~
1067node fast-glob sync 0m0.573s 160023
1068node globby sync 0m0.731s 160023
1069node current globSync mjs 0m0.680s 180023
1070node current glob syncStream 0m0.659s 180023
1071~~ async ~~
1072node fast-glob async 0m0.345s 160023
1073node globby async 0m0.476s 160023
1074node current glob async mjs 0m0.427s 180023
1075node current glob stream 0m0.388s 180023
1076
1077--- pattern: './{*/**/../{*/**/../{*/**/../{*/**/../{*/**,,,,},,,,},,,,},,,,},,,,}/*.txt' ---
1078~~ sync ~~
1079node fast-glob sync 0m0.483s 0
1080node globby sync 0m0.512s 0
1081node current globSync mjs 0m0.811s 200023
1082node current glob syncStream 0m0.773s 200023
1083~~ async ~~
1084node fast-glob async 0m0.280s 0
1085node globby async 0m0.299s 0
1086node current glob async mjs 0m0.617s 200023
1087node current glob stream 0m0.568s 200023
1088
1089--- pattern: './*/**/../*/**/../*/**/../*/**/../*/**/../*/**/../*/**/../*/**/*.txt' ---
1090~~ sync ~~
1091node fast-glob sync 0m0.485s 0
1092node globby sync 0m0.507s 0
1093node current globSync mjs 0m0.759s 200023
1094node current glob syncStream 0m0.740s 200023
1095~~ async ~~
1096node fast-glob async 0m0.281s 0
1097node globby async 0m0.297s 0
1098node current glob async mjs 0m0.544s 200023
1099node current glob stream 0m0.464s 200023
1100
1101--- pattern: './*/**/../*/**/../*/**/../*/**/../*/**/*.txt' ---
1102~~ sync ~~
1103node fast-glob sync 0m0.486s 0
1104node globby sync 0m0.513s 0
1105node current globSync mjs 0m0.734s 200023
1106node current glob syncStream 0m0.696s 200023
1107~~ async ~~
1108node fast-glob async 0m0.286s 0
1109node globby async 0m0.296s 0
1110node current glob async mjs 0m0.506s 200023
1111node current glob stream 0m0.483s 200023
1112
1113--- pattern: './0/**/../1/**/../2/**/../3/**/../4/**/../5/**/../6/**/../7/**/*.txt' ---
1114~~ sync ~~
1115node fast-glob sync 0m0.060s 0
1116node globby sync 0m0.074s 0
1117node current globSync mjs 0m0.067s 0
1118node current glob syncStream 0m0.066s 0
1119~~ async ~~
1120node fast-glob async 0m0.060s 0
1121node globby async 0m0.075s 0
1122node current glob async mjs 0m0.066s 0
1123node current glob stream 0m0.067s 0
1124
1125--- pattern: './**/?/**/?/**/?/**/?/**/*.txt' ---
1126~~ sync ~~
1127node fast-glob sync 0m0.568s 100000
1128node globby sync 0m0.651s 100000
1129node current globSync mjs 0m0.619s 100000
1130node current glob syncStream 0m0.617s 100000
1131~~ async ~~
1132node fast-glob async 0m0.332s 100000
1133node globby async 0m0.409s 100000
1134node current glob async mjs 0m0.372s 100000
1135node current glob stream 0m0.351s 100000
1136
1137--- pattern: '**/*/**/*/**/*/**/*/**' ---
1138~~ sync ~~
1139node fast-glob sync 0m0.603s 200113
1140node globby sync 0m0.798s 200113
1141node current globSync mjs 0m0.730s 222137
1142node current glob syncStream 0m0.693s 222137
1143~~ async ~~
1144node fast-glob async 0m0.356s 200113
1145node globby async 0m0.525s 200113
1146node current glob async mjs 0m0.508s 222137
1147node current glob stream 0m0.455s 222137
1148
1149--- pattern: './**/*/**/*/**/*/**/*/**/*.txt' ---
1150~~ sync ~~
1151node fast-glob sync 0m0.622s 200000
1152node globby sync 0m0.792s 200000
1153node current globSync mjs 0m0.722s 200000
1154node current glob syncStream 0m0.695s 200000
1155~~ async ~~
1156node fast-glob async 0m0.369s 200000
1157node globby async 0m0.527s 200000
1158node current glob async mjs 0m0.502s 200000
1159node current glob stream 0m0.481s 200000
1160
1161--- pattern: '**/*.txt' ---
1162~~ sync ~~
1163node fast-glob sync 0m0.588s 200023
1164node globby sync 0m0.771s 200023
1165node current globSync mjs 0m0.684s 200023
1166node current glob syncStream 0m0.658s 200023
1167~~ async ~~
1168node fast-glob async 0m0.352s 200023
1169node globby async 0m0.516s 200023
1170node current glob async mjs 0m0.432s 200023
1171node current glob stream 0m0.384s 200023
1172
1173--- pattern: './**/**/**/**/**/**/**/**/*.txt' ---
1174~~ sync ~~
1175node fast-glob sync 0m0.589s 200023
1176node globby sync 0m0.766s 200023
1177node current globSync mjs 0m0.682s 200023
1178node current glob syncStream 0m0.652s 200023
1179~~ async ~~
1180node fast-glob async 0m0.352s 200023
1181node globby async 0m0.523s 200023
1182node current glob async mjs 0m0.436s 200023
1183node current glob stream 0m0.380s 200023
1184
1185--- pattern: '**/*/*.txt' ---
1186~~ sync ~~
1187node fast-glob sync 0m0.592s 200023
1188node globby sync 0m0.776s 200023
1189node current globSync mjs 0m0.691s 200023
1190node current glob syncStream 0m0.659s 200023
1191~~ async ~~
1192node fast-glob async 0m0.357s 200023
1193node globby async 0m0.513s 200023
1194node current glob async mjs 0m0.471s 200023
1195node current glob stream 0m0.424s 200023
1196
1197--- pattern: '**/*/**/*.txt' ---
1198~~ sync ~~
1199node fast-glob sync 0m0.585s 200023
1200node globby sync 0m0.766s 200023
1201node current globSync mjs 0m0.694s 200023
1202node current glob syncStream 0m0.664s 200023
1203~~ async ~~
1204node fast-glob async 0m0.350s 200023
1205node globby async 0m0.514s 200023
1206node current glob async mjs 0m0.472s 200023
1207node current glob stream 0m0.424s 200023
1208
1209--- pattern: '**/[0-9]/**/*.txt' ---
1210~~ sync ~~
1211node fast-glob sync 0m0.544s 100000
1212node globby sync 0m0.636s 100000
1213node current globSync mjs 0m0.626s 100000
1214node current glob syncStream 0m0.621s 100000
1215~~ async ~~
1216node fast-glob async 0m0.322s 100000
1217node globby async 0m0.404s 100000
1218node current glob async mjs 0m0.360s 100000
1219node current glob stream 0m0.352s 100000
1220```