Wildcards & Globbing
A wildcard is a character that stands for other characters in a file name, so one pattern can match many files: *.txt means "every name ending in .txt". Expanding such a pattern into the matching names is called globbing. All four shells support wildcards, but they differ in which patterns work and, importantly, in who expands them.
The common patterns
| Pattern | Matches | CMD | PowerShell | bash | zsh |
|---|---|---|---|---|---|
* |
any number of characters (also none) | ✔ | ✔ | ✔ | ✔ |
? |
exactly one character | ✔ | ✔ | ✔ | ✔ |
[abc], [0-9] |
one character from a set or range | — | ✔ | ✔ | ✔ |
[!abc] / [^abc] |
one character not in the set | — | — (use -Exclude) |
✔ | ✔ |
{jpg,png} |
each alternative (brace expansion, not really a glob) | — | — | ✔ | ✔ |
**/ |
any depth of sub-folders | — | -Recurse instead |
with shopt -s globstar |
✔ |
hidden (dot) files matched by * |
yes | (Windows: by attribute) | no | no |
Examples with these files: report1.txt, report2.txt, report10.txt, notes.md, photo.jpg, logo.png:
| Pattern | Result |
|---|---|
*.txt |
report1.txt report10.txt report2.txt |
report?.txt |
report1.txt report2.txt (? is exactly one character, so not report10) |
report[0-9][0-9].txt |
report10.txt |
*.{jpg,png} (bash/zsh) |
photo.jpg logo.png |
[!r]* |
logo.png notes.md photo.jpg |
Who expands the pattern?
This is the big difference between the Unix shells and the Windows ones:
bash / zsh: ls *.txt ──shell expands──▶ ls report1.txt report10.txt report2.txt
(ls never sees the *)
CMD / PowerShell: dir *.txt ──passed as is──▶ dir receives "*.txt" and matches it itselfIn bash and zsh the shell replaces the pattern before the command runs, so wildcards work with every program, even echo. On Windows, each command decides whether it understands wildcards: dir, copy, del and most cmdlets do, but a program you wrote yourself receives the literal text *.txt.
Windows (CMD)
CMD knows only * and ?. dir, del, copy, move, ren and for loops accept them:
@echo off
mkdir files
cd files
for %%n in (logo.png notes.md photo.jpg report1.txt report10.txt report2.txt) do type nul > %%n
echo --- *.txt
dir /b /o:n *.txt
echo --- report?.txt
dir /b /o:n report?.txt
echo --- loop over *.txt
for %%f in (*.txt) do echo processing %%~nf
del *.txt
echo --- after del *.txt
dir /b /o:n--- *.txt
report1.txt
report10.txt
report2.txt
--- report?.txt
report1.txt
report2.txt
--- loop over *.txt
processing report1
processing report10
processing report2
--- after del *.txt
logo.png
notes.md
photo.jpg
for %%f in (*.txt) is how batch files process a group of files one by one.
Watch out: on NTFS volumes that still have 8.3 short names, CMD also matches the short name, so
dir *.htmcan listindex.html(its short name isINDEX~1.HTM), anddel *.htmwould delete it. PowerShell, bash and zsh don't have this quirk.
Windows (PowerShell)
PowerShell cmdlets support *, ? and [ ] ranges. The same patterns work in the -like operator, for filtering any text:
New-Item -ItemType Directory files -Force | Out-Null
Set-Location files
'report1.txt', 'report2.txt', 'report10.txt', 'notes.md', 'photo.jpg', 'logo.png' |
ForEach-Object { New-Item $_ | Out-Null }
'--- *.txt'
Get-ChildItem *.txt -Name
'--- report[0-9][0-9].txt'
Get-ChildItem 'report[0-9][0-9].txt' -Name
'--- everything except *.txt'
Get-ChildItem -Exclude *.txt -Name
'--- -like'
'photo.jpg' -like '*.jp?g'
'photo.jpg' -like '*.jp*g'--- *.txt
report1.txt
report10.txt
report2.txt
--- report[0-9][0-9].txt
report10.txt
--- everything except *.txt
logo.png
notes.md
photo.jpg
--- -like
False
True
'*.jp?g' is False because ? needs exactly one character between p and g. -Include and -Exclude take several patterns (-Include *.jpg, *.png), which replaces brace expansion. If a file name really contains [ or ], use -LiteralPath so the brackets aren't read as a range.
Linux & macOS (bash)
Because bash expands the pattern itself, echo is the perfect way to see what a pattern matches:
mkdir files && cd files
touch report1.txt report2.txt report10.txt notes.md photo.jpg logo.png .hidden.txt
echo "*.txt: " *.txt
echo "report?.txt: " report?.txt
echo "report[0-9][0-9]:" report[0-9][0-9].txt
echo "[!r]*: " [!r]*
echo "*.{jpg,png}: " *.{jpg,png}
echo "no match: " *.pdf
echo "quoted: " "*.txt"*.txt: report1.txt report10.txt report2.txt
report?.txt: report1.txt report2.txt
report[0-9][0-9]: report10.txt
[!r]*: logo.png notes.md photo.jpg
*.{jpg,png}: photo.jpg logo.png
no match: *.pdf
quoted: *.txt
Notice four rules:
- Matches come out sorted, and
*doesn't match names starting with a dot (.hidden.txt).shopt -s dotglobchanges that. - If nothing matches, bash leaves the pattern as it is (
*.pdf), so a command may receive a literal*.shopt -s nullglobmakes an unmatched pattern disappear instead. - Quotes turn globbing off:
"*.txt"is just text. That's how you pass a pattern to a program that does its own matching, such asfind . -name '*.txt'. - Brace expansion is not a match:
{jpg,png}produces both words even if the files don't exist. Pattern matching happens afterwards.
** matches sub-folders at any depth after shopt -s globstar (bash 4+; macOS's old /bin/bash 3.2 doesn't have it):
shopt -s globstar
mkdir -p site/css site/js/lib
touch site/index.html site/css/main.css site/js/app.js site/js/lib/util.js
echo site/**/*.jssite/js/app.js site/js/lib/util.js
macOS (zsh)
zsh supports everything above, and **/ works out of the box. Two behaviours differ from bash:
- If a pattern matches nothing, zsh stops with an error (no matches found) instead of passing the pattern on. Add the
(N)qualifier to get an empty result instead, or quote the pattern. - With
setopt extendedglob,^negates a pattern (^*.txt= everything except.txtfiles) and~excludes (*~*.md).
setopt extendedglob
mkdir files && cd files
touch report1.txt report2.txt report10.txt notes.md photo.jpg logo.png
mkdir -p deep/er && touch deep/er/found.txt
print "not txt: " ^*.txt
print "all txt: " **/*.txt
print "numbered: " report<1-9>.txt
print "no pdf: " *.pdf(N) "(empty)"
print *.pdf
print "never printed"not txt: deep logo.png notes.md photo.jpg
all txt: deep/er/found.txt report1.txt report10.txt report2.txt
numbered: report1.txt report2.txt
no pdf: (empty)
/w/main.zsh:9: no matches found: *.pdf
<1-9> is zsh's numeric range: it matches numbers, so report<1-10>.txt would also match report10.txt. A plain print *.pdf stops the script, because an unmatched glob is an error in zsh. You'll meet this when copying commands from bash tutorials into a Mac terminal: curl https://example.com/?page=1 fails with no matches found because ? is a wildcard. Quote such URLs.
Comparison
| Behaviour | CMD | PowerShell | bash | zsh |
|---|---|---|---|---|
| Who expands | the command | the cmdlet | the shell | the shell |
| No match | command reports it | empty result | pattern passed literally | error no matches found |
| Recursive | dir /s, for /r |
-Recurse |
** with globstar |
** |
| Turn off | n/a | -LiteralPath |
quotes | quotes, noglob |
Learn more: Bash Globbing · Batch for Loops · PowerShell Filter & Sort · Quoting & Escaping
Test yourself
Which files does report?.txt match among report1.txt, report10.txt and report.txt?
- All three
report1.txtonlyreport1.txtandreport10.txtreport.txtonly
In bash, you run ls *.pdf in a folder with no PDF files. What does ls receive?
- Nothing; the command is skipped
- The literal text
*.pdf - Every file in the folder
- An error from the shell before
lsruns
Why does echo *.txt list file names in bash but print *.txt in CMD?
- CMD has no
echo - In bash the shell expands wildcards before running the command; in CMD each command must handle them itself
- CMD needs
**instead of* - bash sorts files