Globbing & Brace Expansion
Before Bash runs a command, it expands the command line: it replaces patterns like *.txt with matching file names (globbing) and turns {a,b,c} into separate words (brace expansion). Knowing what expands, and when, lets you work on hundreds of files with one short command.
Glob wildcards
| Pattern | Matches |
|---|---|
* |
Any number of characters (including none) |
? |
Exactly one character |
[abc] |
One of the listed characters |
[a-z], [0-9] |
One character in the range |
[!abc] or [^abc] |
One character not listed |
touch report1.txt report2.txt report10.txt notes.md data.csv Data.csv
echo "*.txt: " *.txt
echo "report?.txt: " report?.txt
echo "report??.txt: " report??.txt
echo "[dD]ata.csv: " [dD]ata.csv
echo "[!r]*: " [!r]**.txt: report1.txt report10.txt report2.txt
report?.txt: report1.txt report2.txt
report??.txt: report10.txt
[dD]ata.csv: Data.csv data.csv
[!r]*: Data.csv data.csv notes.md
The shell does the matching, then passes the resulting names to the command. echo is a great way to preview a glob before using it with rm or mv.
Rules worth knowing
- Globs don't match hidden files (starting with
.) unless the pattern starts with a dot or you enableshopt -s dotglob. *doesn't cross/:*.logonly matches in the current directory.- Results are sorted alphabetically.
- If nothing matches, the pattern is left unchanged (by default).
touch .env visible.txt
echo "*: " *
echo ".*: " .[!.]*
echo "no match:" *.pdf
shopt -s nullglob
echo "with nullglob:" *.pdf*: visible.txt
.*: .env .gitconfig
no match: *.pdf
with nullglob:
.[!.]* matches dotfiles while skipping . and ... (.gitconfig is a settings file that already exists in this sandbox's home directory.) With nullglob, a pattern that matches nothing expands to nothing at all, which is safer in loops. (shopt -s failglob makes it an error instead.)
Recursive globs: **
With shopt -s globstar, ** matches files in all subdirectories:
mkdir -p src/api src/ui/components
touch src/main.py src/api/routes.py src/ui/app.js src/ui/components/button.js
shopt -s globstar
echo "all .js:" src/**/*.js
echo "all .py:" src/**/*.pyall .js: src/ui/app.js src/ui/components/button.js
all .py: src/api/routes.py src/main.py
Extended globs
shopt -s extglob enables pattern operators similar to regular expressions:
| Pattern | Matches |
|---|---|
?(pat) |
Zero or one occurrence |
*(pat) |
Zero or more |
+(pat) |
One or more |
@(a|b) |
Exactly one of the alternatives |
!(pat) |
Anything except the pattern |
shopt -s extglob
touch app.js app.min.js style.css index.html logo.png
echo "not minified js: " !(*.min).js
echo "web files: " *.@(html|css|js)
echo "everything but png:" !(*.png)not minified js: app.js
web files: app.js app.min.js index.html style.css
everything but png: app.js app.min.js index.html style.css
rm !(*.keep) (delete everything except .keep files) is a popular, and dangerous, use. Preview with echo first!
Brace expansion
Braces generate words, whether or not files exist. They are expanded before globs and variables:
echo file{1,2,3}.txt
echo {a..e}
echo {01..10..3}
echo img_{small,large}.{jpg,png}
echo pre{,fix}file1.txt file2.txt file3.txt
a b c d e
01 04 07 10
img_small.jpg img_small.png img_large.jpg img_large.png
pre prefix
| Form | Expands to |
|---|---|
{a,b,c} |
a b c |
{1..5} |
1 2 3 4 5 |
{01..10} |
zero-padded: 01 02 … 10 |
{a..z..2} |
with a step |
x{1,2}{a,b} |
combinations: x1a x1b x2a x2b |
Two everyday uses: backup copies and project skeletons.
echo "port=80" > nginx.conf
cp nginx.conf{,.bak}
ls nginx.conf*
mkdir -p project/{src,tests,docs/{api,guides}}
find project -type d | sortnginx.conf
nginx.conf.bak
project
project/docs
project/docs/api
project/docs/guides
project/src
project/tests
cp nginx.conf{,.bak} expands to cp nginx.conf nginx.conf.bak.
Tilde and other expansions
The full order of expansions is: brace → tilde (~) → parameter/variable, arithmetic and command substitution → word splitting → globbing → quote removal. Quoting turns expansions off:
touch a.txt b.txt
echo ~ "~"
echo *.txt "*.txt"
echo {x,y} "{x,y}"/home/learner ~
a.txt b.txt *.txt
x y {x,y}
Test yourself
Which files does report?.txt match?
- report.txt and report1.txt
- report1.txt but not report10.txt
- All files starting with report
- Only report?.txt literally
What does echo {1..3}{a,b} print?
- 1a 2b 3a
- 1a 1b 2a 2b 3a 3b
- {1..3}{a,b}
- 123ab
By default, what happens when a glob like *.pdf matches no files?
- It expands to nothing
- The pattern is passed unchanged
- Bash prints an error
- It matches hidden files