Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 95 ยท Working with Files

Wildcards and Globbing

Learn shell globbing on Linux: *, ?, [abc], brace expansion, extglob, globstar, dotfiles, nullglob, and why the shell, not the command, expands wildcards.

Typing the name of every file is tedious, and sometimes impossible: how do you delete "all the log files" or "every photo from 2025"? The shell solves this with wildcards, also called globs. A pattern such as *.log stands for every file name that matches it. The mechanism is called globbing (or pathname expansion), and it is one of the most important ideas in the command line, because it explains behaviour that otherwise looks like magic or like a bug.

The key fact, which this whole lesson builds on: the shell expands the pattern, not the command. When you type ls *.txt, ls never sees the star. Bash first replaces *.txt with the sorted list of matching names and only then starts ls with that list. This is different from Windows cmd.exe, where each program expands wildcards itself. You met cp, mv and rm in the previous lesson; with globbing you can point them at many files at once.

The wildcard characters

Pattern Matches
* Any string, including the empty string (but not /, and not a leading dot)
? Exactly one character
[abc] One character from the set
[a-f] One character in the range
[!abc] or [^abc] One character not in the set
[[:digit:]] One character of a POSIX class (alpha, digit, upper, lower, alnum, space, punct)

Let's build a small playground and try them.

Bash The basic wildcards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Read the results carefully. report?.txt matches report1.txt and report2.txt but not report10.txt, because ? is exactly one character. *[!t].txt matches names whose last character before .txt is not a t; none of our names has a t there, so all four .txt files match. And echo * lists everything because echo just prints the arguments the shell gave it. In fact, echo is the best glob debugger you have: it shows what a command would receive.

Globs are not regular expressions

A glob is a much simpler pattern language than a regular expression. In a glob, * means "anything" on its own; in a regex, * means "repeat the previous item". So *.txt is a glob, and .*\.txt would be its regex cousin. Mixing them up is the number one beginner mistake: grep *.txt does not do what you think, because the shell expands *.txt before grep runs. Quote patterns that are meant for a program: grep '*.txt' file.

Where globbing happens, and what it ignores

Three rules cover most surprises.

  1. Slashes are never matched by * or ?. */*.txt means "a txt file one directory down". The pattern is matched one path segment at a time.
  2. A leading dot must be matched explicitly. * does not match .bashrc, .git and other hidden files. .* does, but with a trap: it also matches . and .. (look at the dot star line in the next example, produced by Bash 5.0; Bash 5.2 and later, and some other shells, leave them out). That is how rm -rf .* has deleted people's parent directories. Never use .* with rm -r; use .[!.]* or the dotglob option below.
  3. Matching is case-sensitive by default, and results are sorted (we run with LC_ALL=C, so uppercase letters sort before lowercase; in other locales they may interleave).
Bash Dotfiles, slashes and directories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

With shopt -s dotglob, * now includes hidden files (but still never . and ..). A trailing slash in a pattern (*/) keeps only directories, a handy trick for "list the folders".

In the last lines you may be surprised by [a-z]*.txt printing only lower.txt: in the C locale a range means the ASCII range, so [a-z] excludes uppercase letters. In other locales ranges can behave differently, which is why [[:lower:]] is the portable choice.

When nothing matches

What happens if no file matches *.xyz? By default, the shell leaves the pattern unchanged and passes the literal text *.xyz to the command. Often the command then complains about a file that does not exist, but not always, and this default has caused real bugs.

Bash No match: the pattern stays literal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

There are three behaviours:

  • Default: the literal pattern survives. The loop above runs once with f='*.xyz', which is almost never what you want.
  • nullglob: a pattern with no match expands to nothing. The loop runs zero times. This is the right setting for most scripts, but remember that ls *.xyz then becomes plain ls and lists the current directory!
  • failglob: a non-matching pattern is an error and the command is not run (Bash prints no match: *.xyz on stderr and the status is 1).

In scripts, either enable nullglob and handle empty lists, or test with [[ -e $f ]] || continue inside a loop. Loops are covered in Loops.

Brace expansion: not a glob at all

Curly braces look like wildcards but work differently. Brace expansion is plain text generation that happens before globbing, and it does not look at the filesystem at all.

TEXT
{a,b,c}        a b c
{1..5}         1 2 3 4 5
{01..05}       01 02 03 04 05   (zero padded)
{a..e}         a b c d e
{0..20..5}     0 5 10 15 20     (with a step)
pre{x,y}post   prexpost preypost
Bash Brace expansion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Two patterns are used constantly: mkdir -p project/{src,tests} to create several directories at once, and cp file{,.bak}, which expands to cp file file.bak (a quick backup). Braces can nest, as in docs/{api,guides}. Because braces are expanded first and blindly, echo no-such-{thing,file} prints two names that do not exist, while * and ? only ever give names that do exist.

Brace expansion does not work with variables in the range: {1..$n} prints {1..3} literally, because the variable is expanded after braces. Use seq 1 "$n" or a C-style for ((i=1; i<=n; i++)) loop.

Extended globs and globstar

Bash can do more than classic wildcards, but you must switch the features on with shopt.

shopt -s extglob adds grouping patterns:

Pattern Meaning
?(a|b) zero or one of the alternatives
*(a|b) zero or more
+(a|b) one or more
@(a|b) exactly one
!(a|b) anything except these

shopt -s globstar makes ** match any number of directory levels, including zero, so **/*.txt finds .txt files at every depth.

Bash extglob and globstar

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

!(*.log) lists everything that is not a log file, a feature that otherwise needs find or grep -v. With globstar, **/*.txt finds the .txt files at every depth, including one.txt in the current directory, because **/ may match zero directories. Without globstar, ** behaves like an ordinary *, so **/*.txt only finds files exactly one directory deep (a/x.txt).

Extended patterns have two warnings. First, extglob has to be enabled before the line containing the pattern is parsed; turning it on in the middle of a line or inside an if block that also uses the pattern does not work. Second, ** walks the whole tree before the command starts and builds one huge argument list; for big trees prefer find, which is also faster and can handle millions of files.

Quoting stops expansion

Quotes turn the wildcard characters back into plain characters. This matters in two situations: you want to pass a pattern to a program, or a file name really contains a star or a space.

Bash Quoting vs expansion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Two lessons here. A glob result is a single word per file, even if the file name contains spaces, so for f in *.txt and rm -- *.txt are safe, while splitting the output of ls is not (more in Quoting and Expansion). And a variable holding a pattern is expanded when unquoted: $pattern becomes a file list, "$pattern" stays literal. Programs such as find -name and grep do their own matching, so they need the pattern unexpanded.

The "argument list too long" limit

Because the shell turns * into one argument per file, a huge directory can exceed the kernel's limit on command size (typically a couple of megabytes). Then you see Argument list too long. The fix is not a different glob but a different tool: find . -name '*.tmp' -delete or find ... -print0 | xargs -0 ..., covered in find and xargs.

Glob cookbook

TEXT
ls *.jpg *.png            two kinds of files
ls report-????.pdf        exactly four characters, e.g. report-2025.pdf
rm -- ./*.tmp             safe delete (./ protects names that start with a dash)
cp -r src/. dst/          copy contents, hidden files included
ls -d */                  only directories
ls [A-Z]*                 names starting with an uppercase letter (C locale)
ls !(*.bak)               everything except backups (needs extglob)
mkdir -p proj/{src,docs}  several directories

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Who expands the pattern in ls *.txt?

    Bash replaces *.txt with the sorted list of matching names, then runs ls with those names as arguments. ls never sees the star.
  2. 2. Which pattern matches report1.txt and report2.txt but not report10.txt?

    ? matches exactly one character. report*.txt and report[0-9]*.txt also match report10.txt, and report??.txt matches only report10.txt.
  3. 3. You run for f in *.xyz; do echo "$f"; done in a directory without .xyz files. With default settings, what is printed?

    By default an unmatched pattern is left unchanged, so the loop runs once with the literal text. shopt -s nullglob makes it run zero times.
  4. 4. What does cp config{,.bak} expand to?

    Brace expansion produces one word per alternative: the text before the braces joined with each alternative. An empty first alternative gives config, the second gives config.bak.
  5. 5. Which statement about * is true?

    A glob star matches within one path segment and skips a leading dot unless dotglob is on. Recursion needs globstar (**) or find.

Summary

  • The shell expands wildcards before the command runs; echo pattern shows what the command will get.
  • * any string, ? one character, [abc] and [!abc] sets, [[:digit:]] classes. Globs are not regular expressions.
  • * does not match a leading dot or a slash. Use dotglob for hidden files and never rm -rf .*.
  • With no match the pattern stays literal; use nullglob (empty) or failglob (error) in scripts.
  • {a,b} and {1..5} are brace expansion: text generation without touching the filesystem.
  • extglob adds !(...), @(...), +(...), and globstar makes ** recursive.
  • Quote patterns meant for other programs. For huge trees, use find.

Next: Finding Files: find, locate and which