What does the double-asterisk (**) wildcard mean?

You’re most likely seeing a special feature of some shells that allow wildcard filename patterns to match across directory boundaries, as opposed to a single *, which is a wildcard that matches only within a directory.

If you do not have such a shell, ** will likely be equivalent to *, because “matching zero or more characters followed by zero or more characters” is the same as just “matching zero or more characters”.

But if you do have such a shell, ** will match all files and directories in the current directory and subdirectories, whereas * only matches files and directories in the current directory. (In both cases “dot files”, those with names starting with ., are not matched).

**‘s real power comes when you use it in more specific patterns. For example, you can specify all .txt files no matter what subdirectory they are in with **/*.txt, whereas *.txt only matches those in the current directory.

You should look at the wildcard matching rules for your shell to know for sure what your shell is doing. For example, the bash manual says:

*
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 “https://stackoverflow.com/”, two adjacent ‘*’s will match only
directories and subdirectories.

In recent versions of bash the ‘globstar’ shell option is disabled by default. Enabled via:

shopt -s globstar

I believe zsh also supports this syntax.

It’s important to keep in mind that wildcards are expanded by the shell, not by the ls command. If you type ls **, or ls *.txt, the ls command itself never sees the * characters; it only sees an expanded list of files matching the pattern, just as if you had typed the entire list on the command line.

Leave a Comment