TExcelTAD
Home › Linux & Bash › Globbing & Brace Expansion

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
bash · try it yourself
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 enable shopt -s dotglob.
  • * doesn't cross /: *.log only matches in the current directory.
  • Results are sorted alphabetically.
  • If nothing matches, the pattern is left unchanged (by default).
bash · try it yourself
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:

bash · try it yourself
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/**/*.py
all .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
bash · try it yourself
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:

bash · try it yourself
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.

bash · try it yourself
echo "port=80" > nginx.conf
cp nginx.conf{,.bak}
ls nginx.conf*
mkdir -p project/{src,tests,docs/{api,guides}}
find project -type d | sort
nginx.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:

bash · try it yourself
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?

  1. report.txt and report1.txt
  2. report1.txt but not report10.txt
  3. All files starting with report
  4. Only report?.txt literally

What does echo {1..3}{a,b} print?

  1. 1a 2b 3a
  2. 1a 1b 2a 2b 3a 3b
  3. {1..3}{a,b}
  4. 123ab

By default, what happens when a glob like *.pdf matches no files?

  1. It expands to nothing
  2. The pattern is passed unchanged
  3. Bash prints an error
  4. It matches hidden files