admin管理员组

文章数量:1278787

I just found , which is the most helpful reference I've found.

I keep seeing:

For more on glob pattern syntax, see the node-glob and minimatch documentation.

Yet, I can't seem to find an exhaustive list of the syntax/usage. These tests might be the best reference, yet still not particularly easy to decipher.

It seems I must be missing some critical source of documentation.

I'm wondering the differences between:

path
path/
path/*
path/*.*
path/**
path/**/
path/**/*
path/**/*.*

and any other important variations that are related that I might have omitted. I'm guessing this applies differently when doing a node-glob style matching ('public/**/*.*') and a .gitignore (node_modules), because in the former, you need to explicitly include everything, many layers deep, and in gitignore, this is handled automatically by ignoring any directory. Is this correct?

I just found http://gruntjs.com/configuring-tasks#globbing-patterns, which is the most helpful reference I've found.

I keep seeing:

For more on glob pattern syntax, see the node-glob and minimatch documentation.

Yet, I can't seem to find an exhaustive list of the syntax/usage. These tests might be the best reference, yet still not particularly easy to decipher.

It seems I must be missing some critical source of documentation.

I'm wondering the differences between:

path
path/
path/*
path/*.*
path/**
path/**/
path/**/*
path/**/*.*

and any other important variations that are related that I might have omitted. I'm guessing this applies differently when doing a node-glob style matching ('public/**/*.*') and a .gitignore (node_modules), because in the former, you need to explicitly include everything, many layers deep, and in gitignore, this is handled automatically by ignoring any directory. Is this correct?

Share Improve this question edited Jun 14, 2014 at 19:54 Michael Lewis asked Jun 14, 2014 at 16:44 Michael LewisMichael Lewis 4,3026 gold badges30 silver badges39 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 26

First of all, I have never worked with node-glob or minimatch libraries. But probably I can still help. There's kind of known syntax for glob pattern matching, but frankly, a quick search in Google shows nothing short and clear. Probably this - http://hgbook.red-bean.com/read/file-names-and-pattern-matching.html#id381184 - is the best resource I've found. The article in Wikipedia is exhaustive and not readable - http://en.wikipedia.org/wiki/Glob_(programming).

In short, IMHO for node-glob:

  • * - stands for any number of characters for a filename, but can't stand for /
  • ** - same as * but crosses folder boundaries
  • [abxy] - can replace any one character from a list; [0-9] can stand for any number

Hence to your example:

  • path/* - all files and folders in path not recoursive
  • path/** - everything in path recoursively
  • path/*.* - all files and folders with point in name; matches a.txt, .hidden, noextension., folder.out, ...

From minimatch documentation - https://github.com/isaacs/minimatch, - it does the same, but utilizes richer and slightly more difficult syntax of Regular Expressions. You may look here for a comprehesive reference - http://www.w3schools.com/js/js_regexp.asp. In short, path/.* stands for anything below the path, but it's not clear if recursive or not. You may probably test it.

To answer the specific question asked here about what the given patterns mean, let's assume a couple of things:

  1. globstar pattern matching is used, which makes ** (double asterisk) pattern matching work. If you're using this in Linux, you can work with the globstar option using shopt (shell options) like this:
shopt globstar        = shows if the globstar option is on or off
shopt -s globstar     = enables the globstar option for the current session
shopt -u globstar     = disables the globstar option for the current session

globstar pattern matching is normally disabled by default, but you can edit your .bashrc file or the like if you want it to be enabled by default instead.

  1. path is a subdirectory within the current directory
  2. we're using the patterns below with the ls command:
path        = all of the contents of the 'path' directory, but no subdirectories
path/       = the same as 'ls path'
path/*      = show all items in the 'path' directory and in any immediate subdirectories
path/*.*    = the same 'path/*', but only for items that have a '.' in the name
path/**     = show all items in the 'path' directory and in all of its subdirectories, showing full paths first then individual directory contents
path/**/    = the same as 'ls path/**', but doesn't show full paths
path/**/*   = the same as 'ls path/**', but without showing the items within the 'path' directory
path/**/*.* = the same as 'ls path/**/*', but only for items that have a '.' somewhere in the item's name

Note: "Items" includes both files and directories.

Here are a few other patterns explained:

path/*.txt    = all items that end with '.txt' within the 'path' directory
path/**.txt   = the same as 'ls path/*.txt'
path/*/*.txt  = all items that end with '.txt' that are within an immediate subdirectory of the 'path' directory
path/**/*.txt = all items that end with '.txt' within the 'path' directory and all of its subdirectories

Thus, if you want to find all of the *.txt files in a directory or any of its subdirectories, using ls **/*.txt makes that pretty easy.

However, keep in mind that there are other options which may affect how the above works and not all systems have implemented glob matching the exact same way, so the above may not hold true for all cases.

For other glob patterns and information about them, here are some other handy resources:

  • https://teaching.idallen.com/cst8207/15w/notes/190_glob_patterns.html
  • https://man7.org/linux/man-pages/man7/glob.7.html
  • https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html

Enjoy!

本文标签: nodejsNodeJavaScript glob filepath matching syntaxwildcardsetcStack Overflow