How do /** and /* differ in terms of directory navigation in Grunt?

1.9k Views Asked by At

This is quite an easy one for you guys, but I can't find a definitive/formal answer to this question.

Suppose we are in directory A. Then,

"A/* " probably means: Every file and folder directly inside A.

"A/** " then may mean: Every file and folder inside A, and every file and folder directly inside every child that is directly inside A. (Basically, an extension of /* operator that traverses one level deeper of the root folder? aka "/** " = "/* /* " )

My "directly inside" terminology might be wrong. May be its better to say "direct child" or something, but you get the idea.

Then, what does "A/**/* " mean? Is it equal to "A/* /* /* " ?

Although this seems basic, its quite confusing when I don't have a formal definition of the operators.

I'm currently using Javascript and trying to modify a Gruntfile. But I guess these operators may come up in any context.

2

There are 2 best solutions below

6
On BEST ANSWER

This behavior is not intrinsic to JavaScript and is not related to any operators: as far as JavaScript is concerned, it is just a string.

The handling of such glob expansion is determined by the specific library/consumer. For gruntjs it is covered in Grunt Globbing Patterns:

It is often impractical to specify all source filepaths individually, so Grunt supports filename expansion (also know as globbing) via the built-in node-glob and minimatch libraries ..

  • * matches any number of characters, but not /

  • ** matches any number of characters, including /, as long as it's the only thing in a path part

All most people need to know is that foo/*.js will match all files ending with .js in the foo/ subdirectory, but foo/**/*.js will match all files ending with .js in the foo/ subdirectory and all of its subdirectories.

As such (but refer to the specific documentation!), /**/ generally means "match any depth of directories" and /*/ or /* means "match a single directory or file part".


The gruntjs documentation is a bit vague on the specific mechanics of ** in the standard "/**/*.x" pattern, but referring to node-glob says:

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.

[.. The double-star character] 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.

Using this knowledge we get the equivalency (when used as a path component), of A/**/f with A/f, A/*/f, A/*/*/f, etc for every number of intermediate directories.

2
On

If you see A/**/* that means to recursively search all the way down the tree of every folder under folder A. For more information look up basic linux style file commands.