Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do double-asterisk (**) wildcards mean?

I've tried the following command but I am having trouble interpreting the results:

ls ** 

I'm not sure exactly what it is outputting and why.

like image 700
Mike Blair Avatar asked Jan 27 '15 17:01

Mike Blair


People also ask

What do double asterisks mean?

Enclosing a phrase between two asterisks is used to denote an action the user is "performing", e.g. *pulls out a paper* , although this usage is also common on forums, and less so on most chat rooms due to /me or similar commands.

What is an * asterisk symbol in Linux a a wild card be an arithmetic operator?

The asterisk ( * )The asterisk represents any number of unknown characters. Use it when searching for documents or files for which you have only partial names. For most web search engines, wildcards increase the number of your search results.

What do asterisks mean in file path?

When specifying file names (or paths) in Microsoft Windows and Unix-like operating systems, the asterisk character (*) substitutes for any zero or more characters, and the question mark (?) substitutes for any one character.

Is wildcard a character?

In software, a wildcard character is a kind of placeholder represented by a single character, such as an asterisk ( * ), which can be interpreted as a number of literal characters or an empty string.


2 Answers

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 '/', 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.

like image 105
Keith Thompson Avatar answered Oct 01 '22 00:10

Keith Thompson


Globbing

By using the double asterisk (**), you are using a glob to list files on a filesystem. A glob is a string of literal or wildcard characters used for matching the file paths. Using one or more globs for locating files on a filesystem is called globbing.

Apart from Linux shells, globbing is also used in various configuration files to specify the list of files to locate. For example: files and folders to ignore in the .gitignore file, files and include options in tsconfig.json file in Typescript projects etc.

Following are some of the most important aspects of the globbing and double asterisk (**) is one of them:


Segments and Separators (/)

The separator is always the / character. A segment is everything that comes between the two separators.

Example: Tests/HelloWorld.js

Here, Tests and HelloWorld.js are the segments and / is the separator.


Single Asterisk (*)

Single Asterisk (*) matches zero or more characters within one segment. It is used for globbing the files within one directory.

Example: *.js

This glob will match files such as HelloWorld.js but not files like Tests/HelloWorld.js or Tests/UI/HelloWorld.js


Double Asterisk (**)

Double Asterisk (**) matches zero or more characters across multiple segments. It is used for globbing files that are in nested directories.

Example: Tests/**/*.js

Here, the file selecting will be restricted to the Tests directory. The glob will match the files such as Tests/HelloWorld.js, Tests/UI/HelloWorld.js, Tests/UI/Feature1/HelloWorld.js.


Question Mark(?)

Question mark(?) matches a single character within one segment. When some files or directories differ in their name by just one character, you can use the ?.

Example: tests/?at.js

This will match files such as tests/cat.js, test/Cat.js, test/bat.js etc.


Square Brackets ([abc])

Square Brackets ([...]) globs the files with a single character mentioned in the square brackets.

Example: tests/[CB]at.js

This glob will match files like tests/Cat.js or tests/Bat.js


Square Brackets Range ([a-z])

Square Brackets Range ([a-z]), matches one character specified in the range.

Example: tests/feature[1-9]/HelloWorld.js

This glob will match files like tests/feature1/HelloWorld.js, test/feature2/HelloWorld.js and so on... upto 9.


Negation (!)

Negation (!) can be used for excluding some files.

Example 1: tests/[!C]at.js

This will exclude the file tests/Cat.js and will match files like tests/Bat.js, tests/bat.js, tests/cat.js.

Negation is also used in configuration files inside an array to negate or exclude some files.

Example 2: ['Tests/**/*.js', '!Tests/UI/**']

This will exclude all files and folders from Tests/UI directory.


That's it! Hope that helps!

like image 37
Yogesh Umesh Vaity Avatar answered Sep 30 '22 23:09

Yogesh Umesh Vaity