113

This is a pretty dumb question, but I haven't really been able to find a satisfactory answer: How do I use gulp globbing to select all files in all subdirectories below a certain directory?

I've tried:

'./src/less' './src/less/' './src/less/*'

None of them seem to work.

Jehan
  • 2,701
  • 2
  • 23
  • 28

2 Answers2

184

The pattern for all files under all directories is usually ./src/less/**/*.* or ./src/less/**/*, either should work.

Generally speaking, it's better to match specific files extensions — even if they should all be the same — to prevent grabbing system files or other junk. In that case, you can do ./src/less/**/*.less for just .less files, or something like .src/less/**/*.{less,css} for both .less and .css files.

The Grunt website has a pretty good reference for the majority of minimatch globs. (Both Grunt and gulp use minimatch, since it's the glob library for pretty much everything Node related.)

It would be nice for gulp or minimatch to have their own complete docs, but that's open source for you.

FelipeAls
  • 21,711
  • 8
  • 54
  • 74
OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • I'll check out the Grunt website, thanks. The point about the system files definitely makes sense. The reason I want it to be a little laxer is because I'm using it for watch. – Jehan Feb 11 '14 at 06:47
  • 1
    If you aren't already, I highly recommend learning and using the [`gulp-watch`](https://npmjs.org/package/gulp-watch) module for watching files, because it can handle watching for new files as well. It's a little more work to set up, but worth it, IMO. – OverZealous Feb 11 '14 at 17:01
  • 8
    For future reference, I use a tool at http://www.globtester.com/ , it uses minimatch, which gulp uses by way of node-glob. "glob refers to node-glob syntax or it can be a direct file path." (source: https://github.com/gulpjs/gulp/blob/master/docs/API.md ) "[node-glob] uses the minimatch library to do its matching."( source: https://github.com/isaacs/node-glob ) – Daniel Dropik Jul 22 '15 at 03:09
1

'./src/less/**' seems to work. Still, if someone has a more definitive list of all globbing commands, I would be happy to accept your answer and add it to the gulp docs. Right now you have to go to the docs for one of gulp's submodules, which then gives you a list of manpages. It would be good to have a direct reference, especially for designers using gulp.

Jehan
  • 2,701
  • 2
  • 23
  • 28
  • 1
    `gulp.watch('./build/**', ...)` seems to (mis?)trigger for some changes in the *parent* directory (e.g., ./.git/). Adding the extra /* on the end seems to avoid that problem: `gulp.watch('./build/**/*', ...)`. – medmunds Jul 02 '15 at 00:35
  • @medmunds I have found similar results. The glob HAS to end with a * or a specific \*.less. \*.\* will not work as it misses anything without an extension. It appears to take the *.* and match using POSIX style matching; meaning if a file does not have a 'period' in it, it will not match. Perhaps there_ought_ to be a special case for *.* as *.* via DIR on Windows matches 'LICENSE', but unfortunately `ln *.*` does not. Two different meanings to '*.*' is sad. `*` seems to do what we need. A *.* glob is probably never what the developer really intended in my opinion and may need a warning thrown. – Andrew T Finnell Oct 04 '16 at 13:42
  • @filthy_wizard - `**` is recursive. – Steven Ventimiglia Jul 30 '18 at 03:28
  • Unless there's a bug, `**` should not catch anything outside the intended directory subtree. The only difference is that `**/*` excludes the base directory itself. – webninja Jul 16 '21 at 21:51