$_ CLI In Depth
Learning progress 0%
lesson files globbing find

Glob Patterns & Brace Expansion

6 min read Module 4: File Manipulation, Globbing & find Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

Expansion happens before the program starts

When you type ls *.txt, ls never sees the asterisk. Bash expands the pattern against the directory and replaces the word with the matching names, which are passed to ls as separate arguments. If nothing matches, bash passes the pattern through unchanged unless a shell option says otherwise. The program receives only words, and it cannot tell whether a word came from a glob.

This is why globs are safe for building argument lists. The names are passed as separate arguments with their spaces intact, provided that you quote the expansion. It is also why globs are the preferred way to process files in a script, instead of parsing the output of ls.

The basic glob characters

An asterisk matches any string, including the empty string, but not a leading dot. A question mark matches exactly one character. A bracket expression such as [abc] matches one character from the set, and [a-z] matches a range according to the collation of the locale. An exclamation mark inside the brackets, as in [!abc], negates the set. The leading dot rule is important: a hidden file is matched only by a pattern that begins with a dot, so the normal listing ignores files such as .profile.

BASH5 lines
cd /workspace
mkdir -p globs
touch globs/a.txt globs/b.txt globs/c.log globs/.hidden.txt
echo globs/*.txt
echo globs/?.log

The first command prints two names, and the hidden file is not among them. The second matches the single-character name c.log.

Brace expansion is textual

Braces are a different mechanism. Bash expands {a,b,c} into three words, and the result does not depend on the file system. The command echo {a,b}.txt prints a.txt and b.txt even if neither exists. Brace expansion is also how you write sequences, such as {1..5} or {a..e}, and it can nest, so a single command can build a tree of directory names.

BASH4 lines
cd /workspace
echo {a,b}.txt
mkdir -p backup/{2025,2026}/{jan,feb}
ls backup/2026

Brace expansion happens first, which is why the command creates four directories in one step. Each combination becomes one path, and mkdir -p creates the missing parents.

The order of expansions

Bash performs several expansions in a fixed order: brace expansion, then tilde expansion, parameter and command substitution, arithmetic, word splitting, and pathname expansion, which is glob matching. Brace expansion happens before globbing, so echo g{a,b}* first becomes two patterns and then matches each. Word splitting happens before globbing as well, which is why an unquoted variable containing an asterisk is expanded into matches. Quoting a variable stops word splitting and globbing, and this is why quotes are essential around variables that hold names.

Note

A glob that matches nothing is left as written by default. The command echo *.zip in an empty directory prints the literal string *.zip. Extended globs and nullglob, which the next unit covers, change this behavior.

Sorting and locale

Glob matches are sorted according to the collation of the current locale. In the C locale, uppercase letters sort before lowercase ones, and punctuation sorts by byte value. In a UTF-8 English locale, letters are compared more loosely, and the order of names that differ only in case or punctuation can change. A script that depends on order should set the locale or sort the output explicitly.

Why a glob is not a regular expression

Globs and regular expressions look similar, and they are different languages. A glob describes names and always matches a whole word, so the pattern *.txt means any name that ends in .txt. A regular expression describes text and can match anywhere in a line, so the same letters in grep mean something else. The question mark in a glob matches one character, while in a regular expression it makes the previous item optional. Mixing them is a frequent source of mistakes, especially in commands such as grep and find that take one or the other.

When you pass a glob to a command that expects a regular expression, the glob is expanded first by the shell. The command then receives the list of names, and the expression is applied to something it was not written for. Put patterns for grep in quotes so the shell does not expand them, and use find -name for glob matching on names.

Building paths with braces and variables

Brace expansion combines well with variables. The command mkdir -p "$base"/{src,test,docs} creates three directories under a base path, and the quotes keep the base intact when it contains spaces. Brace expansion runs before parameter expansion, so the list inside the braces is fixed text, and only the variable outside the braces is filled in later. Keep the list literal and the variable outside, as shown here, because a list stored in one string variable is a single word and is not split into the braces' items.

Sequences are handy for numbered files. The expansion file{1..3}.txt gives three names, and {01..10} keeps the leading zeros, which matters for sorting. A step can be given as in {0..10..2}, which counts by twos. These forms make test data easy to generate without a loop.

Globs in loops and conditions

A glob is most often used in a for loop, as in for f in *.log; do ...; done. The loop receives each matching name in turn, and the shell does the directory read for you. Quote the variable inside the loop, as in "$f", so that a name with spaces remains one argument. A loop over a glob is the clearest way to process a small, known set of files, and it avoids the parsing hazards of ls.

The test command can check a glob too, but only through a loop or an array, because a glob that matches several names gives a test several words. The usual check is to see whether a pattern matched anything, which the nullglob unit covers in detail. Until you have that option, test the first match with the -e test, which is false when the literal pattern was returned.

Practice in the lab

The lab creates a few files, matches them with a glob, and creates a set of directories with one brace expression. The checks compare the words the shell produced, so you can see exactly what the expansion gave.

Globs, brace expansion and dotfiles

Visualizer

The shell expands a pattern against the directory listing before the command runs. Brace expansion happens earlier, and it never looks at the disk.

1 / 5
$ echo {a,b}*.txt
ls of the test directory
a1.txt
a2.log
b.txt
notes.md

Nothing has been expanded yet. The directory listing below is what the glob step will read.

cd "$(mktemp -d /tmp/glob.XXXX)" && touch a1.txt a2.log b.txt notes.md && echo {a,b}*.txt
Terminal lab · 3 steps · real Linux in your browser

Match names and expand braces

See what a glob matches, use a single-character glob, and create a grid of directories with braces.

01
Match text files
Save what the pattern globs/*.txt expands to: ``bash echo globs/*.txt > txt-list.txt ``
Show hint for step 1
The shell expands the pattern, so echo prints the matching names.
02
Create a grid of directories
Create the four directories backup/2025/jan, backup/2025/feb, backup/2026/jan and backup/2026/feb with one brace expression: ``bash mkdir -p backup/{2025,2026}/{jan,feb} ``
Show hint for step 2
Two brace groups inside one word produce every combination.
03
Match one character
Save what globs/?.log expands to: ``bash echo globs/?.log > question.txt ``
Show hint for step 3
The question mark matches exactly one character.
Check yourself Question 1 of 4
Concept Check Matching one character recall

Which glob character matches exactly one character?

Recap & Key Takeaways

A glob is expanded by the shell against existing names before the program starts, and a pattern with no matches is passed through unchanged.
Patterns do not match a leading dot unless the pattern starts with a dot, so hidden files are excluded by default.
Brace expansion is textual and does not check the file system, so it can build paths for directories that do not exist yet.
Expansion order matters: braces, then word splitting, then globbing, and quotes stop the last two.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue