Jay Taylor's notes
back to listing indexbash - What does the double-asterisk (**) wildcard mean? - Stack Overflow
[web search]-
-
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 typels **
, orls *.txt
, thels
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.current diranywhere anywhere
underx/o
anywhere
under anyo
only directly
under anyo
*.md
**/*.md
x/o/**/*.md
**/o/**/*.md
**/o/*.md
f.js
f.md
✅ ✅ x
x/f.js
x/f.md
✅ x/o
x/o/f.js
x/o/f.md
✅ ✅ ✅ ✅ x/o/z
x/o/z/f.js
x/o/z/f.md
✅ ✅ ✅ x/y
x/y/f.js
x/y/f.md
✅ x/y/o
x/y/o/f.js
x/y/o/f.md
✅ ✅ ✅ **.md
is the same as*.md
**.md
works like*.md
, not like**/*.md
. If you append or prepend anything to**
other than/
, it will work exactly the same as*
.-
2
-
4@pablete:
*
covers also files with an asterisk in its name. If you want to select such files you may usels *'*'*
(just put quotes around the asterisk) Commented Jul 2, 2021 at 8:08 -
-
it seems that if you simply prepend
/
to**
, it also behaves like*
. for example `./**' only shows the files one level deep. Commented Sep 4, 2022 at 17:40 -
1@user2514157, It is only a convention, not a rule, that files have extensions and directories do not. Also, there are other file system types besides files and directories. So glob patterns cannot make any assumptions, and only match on the names, not types. You will find an answer to your needs by searching Stack Overflow for "glob only directories files".– InigoCommented Nov 5, 2023 at 16:39
The exact behavior of this particular wildcard has been well covered by the other answers, but information on the general case may be useful.
This behavior is not limited to
ls
, and is referred to as "globbing", which is the expansion of patterns based on matches with existing filenames. It is important to note that these patterns do not use regular expression syntax.The shell pre-processes the arguments before they are sent to the program. There are generally multiple levels of expansion, some of these involve globbing.
A great resource for more information on the other wildcards available in a file glob pattern is the unix manpage. A online version for glob can be found here.
Finally, a simple example of what this can do for you, especially when combined with other shell expansion goodies, in this case those provided by the
bash
shell. Information about the expansions used in this example can be found in the Bash Guide for Beginners - which is my goto resource, despite the title.ls *{01..04}.{txt,csv}
becomesls *01.txt *01.csv *02.txt *02.csv *03.txt *03.csv *04.txt *04.csv
Which could output something like this:
input_01.txt input_02.txt input_03.txt input_04.txt output_01.csv output_02.csv output_03.csv output_04.csv
While skipping these:
input_05.txt input_06.txt input_07.txt input_08.txt input_09.txt input_10.txt output_05.csv output_06.csv output_07.csv output_08.csv output_09.csv output_10.csv
A trivial example, but if you know that this behavior is not specific to
ls
, then you can imagine the utility when coupled withmv
,cp
,rsync
, etc.Your Answer
Post as a guest
NameEmailRequired, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged or ask your own question. -
-
- The Overflow Blog
-
-
- Featured on Meta
-
-
-
-
Linked
Related
Hot Network Questions
- Select unique row and column entry from an nxn matrix
- Is Caesar's Bellum Gallicum the same as Commentarii de Bello Gallico?
- Are these trill instructions correct?
- Disadvantages of posting on arXiv when submitting to Nature or Science?
- Capacitance multiplier as a reservoir capacitor
- Running air handler in fan-only mode
- Does a vector change under coordinate transformation?
- looking for a word meaning trimming eyebrows
- Does painting or staining a fence make it last longer?
- What is the difference between and Indefinite Adjectives and Indefinite Pronouns?
- Which is better to prolong li-ion battery life: float charged at 80% SoC or cycle between 20% - 80% SoC?
- Stick lodging into front wheel - is it preventable?
- Roll a die in 3D
- Where to put a 300Ω to 50Ω
- What happens when a car starts moving? The last moment the car is at rest versus the first moment the car moves
- How to mitigate fading of SW reception
- Depth of field doesn't work in Cycles only when I import external files
- My Bee Nests have no honey and no bees inside them, but also don't stack, what's going on?
- How might non-terrestrial environments promote health better than Earth-like conditions?
- Why do some nations hold close ties with their former colonies and some don't?
- How to fix frayed Magsafe 3 cable?
- Alternative solution to Meterological weather forecasting systems
- How to cover up a kitchen pass-through window without permanent changes?
- Is there anything like a poll on the most plausible or convincing arguments for believing in God's existence?