Mastering Glob patterns: from regex lite to powerful file finders


Glob patterns, often seen as simpler cousins of regular expressions, play a crucial role in various programming tasks, especially when dealing with files and directories. They offer a concise and versatile way to define a set of files matching specific criteria, saving you from writing complex and error-prone code. This article delves into the world of glob patterns, exploring their functionalities and showcasing their usage in both Java and JavaScript’s Gulp framework.

Understanding Glob syntax

Glob patterns utilize a set of special characters to represent different file/directory elements. Here are the key players:

  • *: Matches any sequence of characters (except dots at the beginning of a name).
  • ?: Matches a single character.
  • []: Represents a character class, where characters within square brackets match any single character inside.
  • {}: Specifies expansion options, similar to | in regex.
  • {extension}: Matches specific file extensions.
  • /: Separates directories in a path.

For example, *.txt matches all files ending with .txt, while dir1/myfile.{js,py} matches both myfile.js and within the dir1 directory.

Java: leveraging Glob patterns with streams and libraries

Java 8, with its focus on functional programming, provides powerful tools for working with globs. Let’s see some examples:

  1. Finding all .java files in a directory using Files class:
    .filter(p -> p.toString().endsWith(".java"))

  1. Using Apache Commons Lang for more complex patterns:
FileSet fileset = new GlobFileSet("target", "**/*.class");
for (File file : fileset.getFiles()) {

  1. Iterating over matching files and performing actions:
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.txt");
Stream<Path> files = Files.find(Paths.get("data"), Integer.MAX_VALUE, (path, basicFileAttributes) -> matcher.matches(path));
files.forEach(path -> System.out.println(path.getFileName()));

Gulp: streamlining build tasks with Globs

Gulp, a popular JavaScript task runner, heavily relies on glob patterns to define file sets for various processing tasks. Here’s a glimpse into its world:

  1. Copying all .css files from src to dist:

  1. Minifying all JavaScript files with specific extensions:
gulp.src(['src/**/*.js', '!src/**/*.min.js'])

  1. Transforming Sass files within specific directories:

  1. Watching for changes and performing actions:'src/**/*.js', (event) => {
    gulp.src(event.path, { base: 'src' })

Beyond the basics: advanced Glob usage and tips

While the above examples showcase basic usage, there’s more to globs. Here are some advanced tips:

  • Negative patterns: Use ! to exclude matches (e.g., !*.md).
  • Nested globs: Define patterns within directories (e.g., dir1/(file*.txt|*.json)).
  • Combining with libraries: Explore external libraries for more extensive patterns (e.g., minimatch).


  • Use clear and descriptive patterns for maintainability.
  • Test your patterns thoroughly to avoid unintended file selections.
  • Consider performance implications when dealing with large datasets.

Conclusion: the power of Glob patterns

Glob patterns offer a powerful yet accessible way to navigate and manipulate files in your projects. Mastering their syntax and leveraging them effectively in Java 8 and Gulp can significantly streamline your development workflow. So, embrace the simplicity and efficiency of globs, and watch your productivity soar!

Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.