Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:02:14+00:00 2026-06-09T20:02:14+00:00

In Java7, sun.nio.fs.Globs and getPathMatcher() seem to understand the idiom ** as a way

  • 0

In Java7, sun.nio.fs.Globs and getPathMatcher() seem to understand the idiom ** as a way to match zero or more characters across directory boundaries (see the getPathMatcher javadoc).

I could swear some flavor of shell (zsh, bash, tcsh) with some appropriate option settings was giving me the same behavior at some point. But for the life of me I cannot remember how to enable this, and I’m even starting to doubt my memory that I had it working at some point… (Edit: zsh provides that behavior, but only for directories, i.e. "**.gz" doesn’t match foo/bar/fubar.gz, but "**/*.gz" does).

In fact, looking at the documentation for various implementations of glob (e.g. POSIX glob(3), glob(7), and Perl’s File::Glob) this behavior doesn’t seem mentioned anywhere. One exception is Ruby’s Dir.glob() which explicitly handles **.

(original question was: “Does anyone know how to enable this behavior in a unix shell (e.g. zsh)?”, but now see the Edited question below).

As a bonus question: does anyone know how to search for '**' in Google?…


Edited question

In fact, it looks like that behavior is indeed accepted by my zsh shell (thanks for the responses asserting that fact and prompting me to look further). The reason I thought it wasn’t comes from the following subtlety: "**.gz" won’t match a <path>/<prefix>.gz, but "**/*.gz" will. Here is an example. Let’s start with the following tree:

$ find . -type f | sort
./foo/a.gz
./foo/bar/fubar/abc.gz
./foo/bar/x.gz
./foo/bar/y.gz
./xyz.gz

"**.gz" doesn’t match inside subdirs, just matches what "*.gz” would:

$ ls -1 **.gz
xyz.gz

whereas "**/*.gz" does:

$ ls -1 **/*.gz
foo/a.gz
foo/bar/fubar/abc.gz
foo/bar/x.gz
foo/bar/y.gz
xyz.gz

Now, compare this to the Java behavior:

@Test
public void testStar() {
    String pat = Globs.toUnixRegexPattern("*.gz");
    assertEquals("^[^/]*\\.gz$", pat);
}

@Test
public void testStarStar() {
    // '**' allows any number of directories on the path
    // this apparently is not POSIX, although darn useful
    String pat = Globs.toUnixRegexPattern("**.gz");
    assertEquals("^.*\\.gz$", pat);
}

Clearly (from the regexps), here the "**" matches any character on the path (i.e. it becomes ".*" in the regexp), whether in a subdirectory or not, and as part of filename or not.

(Disclaimer: Globs is a copy of sun.nio.fs.Globs.toUnixRegexPattern(String glob) because I needed something that works cross-platform).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:02:15+00:00Added an answer on June 9, 2026 at 8:02 pm

    In POSIX shell:

    The slash character in a pathname shall be explicitly matched by using
    one or more slashes in the pattern; it shall neither be matched by the
    asterisk or question-mark special characters nor by a bracket
    expression

    You could google: “filename expansion pattern”.

    In bash you could set globstar:

    [An asterisk] Matches any string, including the null string. When the globstar shell
    option is enabled, and ‘*’ is used in a filename expansion context,
    two adjacent ‘*’s used as a single pattern will match all files and
    zero or more directories and subdirectories. If followed by a ‘/’, two
    adjacent ‘*’s will match only directories and subdirectories.

    $ shopt -s globstar
    $ ls **/
    $ shopt -u globstar
    $ ls **/
    

    note: ‘/’ is used here to show only directories.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using java nio selector, and seem to hit the following issue randomly
I'm learning java nio, and found the implementation classes are under sun.nio , which
I am using http://java.sun.com/jsp/jstl/fmt tag library for outputting messages from Message Resource Bundle which
If I use SMTPTransport ( http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/SMTPTransport.html ) as private static object for class that
I'm following this MVC model: http://java.sun.com/developer/technicalArticles/javase/mvc/ In my model I have an ArrayList shapes
<%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> <td colspan=1 width=100> <c:choose> <c:when> <c:choose> <c:when></c:when><c:otherwise> <c:when test=<%=\F\.equals(result[1]) %>>
<%@ taglib prefix=c uri=http: java.sun.com/jstl/core %> this doesn't work. What is the new taglib
I have a piece of code VERY similar to this one http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailFetching I the
I have this JSP code snippet: <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> <c:choose> <c:when test=${var1.properties[\Item Type\]
My web.xml is like <web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd> <display-name>sample</display-name> <servlet> <servlet-name>Sampleclass</servlet-name> <servlet-class>sample.SampleClass</servlet-class>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.