Globbing and wildcards
The shell, not the command, expands *, ? and [...] into file names. See hidden files, unmatched patterns and quoting, plus globstar, nullglob and extglob.
Globbing and wildcards
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The shell expands a glob pattern into a sorted list of matching file names before the command runs. The command never sees the pattern.
- * matches any run of characters, ? exactly one, and [abc] or [a-z] one character from a set. [!x] or [^x] negates the set.
- A * does not match names that start with a dot, and does not cross a slash. Quoting the pattern turns the expansion off.
- A pattern with no match is passed through literally by default. nullglob makes it expand to nothing and failglob makes it an error.
- ** only recurses after shopt -s globstar. Patterns like !(*.txt) and *.@(md|csv) need shopt -s extglob, set on an earlier line.
Typing fewer names
You have 200 log files and want to delete the .log ones. Typing 200 names is silly. What you need is a way to describe a set of names. Unix has done this since the 1970s with glob patterns, also called wildcards.
The key idea, and the thing beginners get wrong, is who expands them. It is the shell, not the command. You can see it by handing a pattern to something that does nothing but echo its arguments:
$ ls -A | tr '\n' ' '
.hidden.txt Photo.JPG a.txt b.txt c.md my notes.txt report1.csv report10.csv report2.csv sub
$ printf '[%s]\n' *.txt
[a.txt]
[b.txt]
[my notes.txt]
(I made these files in a scratch directory. Note that ls put Photo.JPG first while the shell’s own expansion, in a later example, sorts it after my notes.txt. The two use different sort rules, so don’t assume a glob’s order matches ls.) printf never saw *.txt. By the time it started, the shell had replaced the pattern with three real names, in sorted order. Each match is one word, so my notes.txt stays whole even though it contains a space. Every command works with wildcards for this reason, including ones written years after the idea.
The three building blocks
*matches any run of characters, including none.?matches exactly one character.[...]matches one character from a set or range. A leading!or^negates it.
$ echo report?.csv
report1.csv report2.csv
$ echo report??.csv
report10.csv
$ echo report[0-9].csv
report1.csv report2.csv
$ echo [!a]*.txt
b.txt my notes.txt
report?.csv skipped report10.csv because ? is exactly one character.
What * won’t match
Two rules cause most of the confusion. Dotfiles are skipped: a leading dot must be matched by an explicit dot in the pattern.
$ echo *
a.txt b.txt c.md my notes.txt Photo.JPG report1.csv report10.csv report2.csv sub
$ echo .[!.]*
.hidden.txt
And * stops at a slash. sub/* matches inside sub, */* matches one level down, and */ matches only directories.
One thing changed recently. Older advice says .* matches . and .. too, so rm -r .* is famously dangerous. On the Bash 3.2 that macOS ships, I saw that:
$ /bin/bash -c 'echo .*'
. .. .hidden.txt
On Bash 5.3, .* gave just .hidden.txt. The globskipdots option, which shopt showed as on in my 5.3.20 and which I believe arrived in Bash 5.2, skips them. The old danger still applies to old Bash and other shells.
Quoting turns it off
$ echo "*.txt"; echo '*.txt'; echo \*.txt
*.txt
*.txt
*.txt
Any quoting prevents expansion. The same is true for a pattern stored in a variable: pat='*.txt'; echo $pat expanded, but echo "$pat" printed the pattern. Quoting is how you pass a pattern to find -name or grep so the shell doesn’t take it first. That’s the topic of the quoting lesson.
When nothing matches
This one surprises people. By default a pattern with no match is passed along literally:
$ echo *.zip
*.zip
$ ls *.zip
ls: *.zip: No such file or directory
echo printed the pattern because it never knew it was a pattern. In a for f in *.zip loop, that means one iteration with f set to *.zip. Three options change this:
$ shopt -s nullglob; printf '[%s]\n' *.zip # no output: it expanded to nothing
$ (shopt -s failglob; echo *.zip)
bash: no match: *.zip
nullglob makes an unmatched pattern vanish, and failglob makes it an error that stops the command. Zsh behaves like failglob by default (zsh:1: no matches found: *.zip), which is a real difference when porting scripts.
Recursion and extended patterns
By default ** is just *. Turn on globstar and it crosses directories:
$ echo **/*.txt
sub/d.txt
$ shopt -s globstar; echo **/*.txt
a.txt b.txt my notes.txt sub/d.txt
The first used ** as a plain *, so it matched only one level down and missed the top-level files. Note that unmatched *.jpg never matched Photo.JPG: matching is case-sensitive unless shopt -s nocaseglob. (On a case-insensitive filesystem like the default on macOS, the file system itself may ignore case when you open it, but the glob doesn’t.)
extglob adds patterns like !(*.txt) (“everything but”) and *.@(md|csv). It must be set on an earlier line than the pattern, because Bash reads a whole line before running it. When I put shopt -s extglob and echo !(*.txt) on one line, the shell failed with a syntax error:
$ shopt -s extglob
$ echo !(*.txt)
c.md Photo.JPG report1.csv report10.csv report2.csv sub
$ echo *.@(md|csv)
c.md report1.csv report10.csv report2.csv
The option trap
A file named -n shows why globs deserve care. With one in the directory, echo * swallowed it, treated it as the no-newline flag:
$ echo *
a.txt b.txt c.md my notes.txt Photo.JPG report1.csv report10.csv report2.csv sub[end]
$ printf '[%s]\n' ./* | head -1
[./-n]
The [end] marker landed on the same line because the newline was suppressed. Prefixing ./ makes every name safe, or end options with --. Patterns also work in case and [[ $f == report?.csv ]], which I ran, and they are covered in later lessons.
About the video
Nick Janetakis’s “Using Extended Globbing for Patterns in Bash” was published 18 March 2025 and runs 10 minutes 19 seconds. Its description gives timestamps: default patterns (0:00), “All files except” (1:35), enabling extglob (1:53), how it works (2:51), checking shopt options (3:56), a specific use case (5:00), an edge case in a script (6:25) and toggling it in a script (8:42). So it covers the basics briefly and extglob in depth. It is Bash-specific. English per its title and description. I couldn’t read a transcript. The nullglob, globstar and no-match sections are mine. It replaced a 2-minute You Suck at Programming video whose description had only links. That channel’s basics and extglob videos are listed as deeper picks.
What I did not run
Linux. GLOBIGNORE, shopt -s dotglob on a large tree, and the ARG_MAX failure that happens when a glob expands to more than the system’s argument limit (1048576 bytes here, from getconf ARG_MAX). I never expanded that many names.
Next
A glob only matches names in the directories you give it. When you need to search a whole tree by name, size or age, use finding files with find.
Check yourself
You run rm *.log in a directory with no .log files. What does rm receive, and what happens?
By default Bash passes the literal text *.log, so rm complains it can't find a file of that name. With nullglob it would receive nothing at all.
Why does ls * not list your .env file?
A leading dot has to be matched explicitly. Use ls .[!.]* for hidden names, or enable dotglob so * includes them.
What is the difference between echo *.txt and echo "*.txt"?
The unquoted form is expanded by the shell into the matching file names. The quoted form prints the pattern itself because quotes turn globbing off.
You have a file named -n. Why does echo * misbehave, and what is the safer way?
The shell expands * to a list starting with -n, which echo treats as an option. Use ./* or put -- before the names so they can't be read as options.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.