SSD Nodes Learn Hosting plans →
How to do am Matt ConnorBy Matt Connor · Updated 2026-10-14

Bash Globbing: Wildcards, nullglob and extglob

Learn how Bash turns wildcards into filenames before commands run, plus the gotchas with nullglob, dotglob, globstar and extglob negation.

Verified Every command ran end-to-end on a fresh Ubuntu 24.04 server, August 14, 2026.

Bash globbing dey do before your command run

Bash globbing na when shell turn pattern like *.log into sorted list of real filenames before command start. rm *.log no dey hand rm any pattern. Bash go read directory, keep the names wey match, sort dem, then run rm with each name as separate argument. rm program no fit know say any pattern ever dey involved.

Almost everything wey surprising about globbing come from this one fact. Shell dey do the matching against files wey exist for that moment, and command dey see only the final list. The formal name for this na pathname expansion. Globbing na the everyday word, and both mean the same thing.

Build small tree wey you fit practise on

Every example wey follow dey use the same files. Create dem for temporary directory, so nothing wey you care about dey nearby, and cleanup go be one command.

lab=$(mktemp -d)
cd "$lab"
mkdir -p logs archive/2025 archive/2026
touch app.log app.log.1 app.log.2 app.log.10 error.log debug.LOG
touch notes.txt notes.md README 'weekly report.log'
touch .env .apprc
touch logs/nginx.log logs/nginx.log.1
touch archive/2025/dec.log archive/2026/jan.log

mktemp -d dey create fresh directory and return the path, while $(...) dey capture that path inside variable lab. If this syntax new to you, how command substitution dey capture command output explain am fully.

Look wetin you build, and check your shell, because one note for the end depend on the version:

ls -a
bash --version

Everything here dey work for bash 4.0 and later. The globskipdots note near the end apply to bash 5.2 and later. As of October 2026, Ubuntu 26.04 LTS dey ship bash 5.3, Ubuntu 24.04 LTS and Debian 13 (trixie) dey ship bash 5.2, while Ubuntu 22.04 LTS still dey ship bash 5.1, wey come before that option.

The preview command wey we use throughout na printf '%s\n' PATTERN. E dey print one match for each line and e no change anything for disk, so e safe to run am against any pattern wey you no sure about.

Wildcards wey dey three: star, question mark, and brackets

  • * dey match any run of characters, including empty one.
  • ? dey match exactly one character.
  • [...] dey match exactly one character wey come from the set inside the brackets.
printf '%s\n' *.log
printf '%s\n' app.log.?
printf '%s\n' app.log.[0-9]
printf '%s\n' notes.*
printf '%s\n' *

Compare the second line with the file list wey you create. ? mean one character and nothing more, so two-digit suffix no fit this pattern. Make am wider to app.log.[0-9]* and run am again to see the difference.

Two rules apply to all three wildcards. No wildcard ever dey match a /, so pattern dey stay inside one directory level. And you must type leading . literally, so pattern wey start with * go skip every hidden name. Na this second rule make rm * leave your dotfiles alone.

Glob results dey come sorted, using collation order of the current locale, wey be character-by-character text sort. So suffix of 10 dey sort before suffix of 2, because comparison no ever read either one as number. Run printf '%s\n' app.log.*, then ls -v app.log.*, and compare both: ls -v dey sort embedded numbers by value, while sort -V dey do the same work inside a pipeline.

Character classes and ranges

Inside the brackets, you fit write a list, a range, or a negation.

  • [ch] dey match one character, either c or h.
  • [a-f] dey match one character for the range a to f.
  • [!0-9] dey match one character wey no be digit. [^0-9] mean the same thing.
printf '%s\n' [dn]*
printf '%s\n' *.[A-Z]*
printf '%s\n' [A-Z]*

Ranges na the part wey fit behave different between machines. The current locale dey use collation order to resolve a range, instead of plain ASCII. So for some systems, [a-z] fit also match uppercase letters. Bash get a globasciiranges option wey force plain ASCII order, and recent builds enable am by default. Check your own setting with shopt globasciiranges instead of assuming. If script must match the same way everywhere, you fit set LC_ALL=C for one line by itself, near the top. This go put every range back to plain byte order.

That assignment must be a command on its own. If you write am as a prefix, LC_ALL=C ls [a-z]* go set the locale for ls only. The shell don already expand the pattern with its own locale before ls start. Na the same rule from the first section show again here.

Brace expansion no be globbing

Braces fit look like wildcard, but dem no dey behave like wildcard at all. Brace expansion dey happen well before globbing, and e no dey check filesystem.

echo file{1,2,3}.txt
echo {01..10}
echo {a..e}
echo {0..20..5}

None of those files dey exist, but expansion still happen. Na this be the main difference: glob dey ask filesystem question, while brace only dey generate text. Na why braces dey useful for creating things instead of finding dem.

mkdir -p site/{css,js,img}
cp app.log{,.bak}
touch report-{2025,2026}-{01,02}.csv

cp app.log{,.bak} dey expand into two words, app.log and app.log.bak, because the empty item before comma dey reproduce the original text. {01..10} keep its zero padding, because leading zero for either endpoint make bash pad every generated number to the same width.

Expansion order still explain one common failure. Bash expand braces first, then variables, and pathname expansion happen last.

n=5
echo {1..$n}

Brace expansion don finish before $n turn into number, so no range ever dey build. Use seq 1 "$n", or use C-style loop written as for ((i=1; i<=n; i++)).

Wetin happen when pattern match nothing

Dis na the first way e fit fail, and e dey catch everybody at least once. By default, bash dey leave pattern wey no match completely as e be. The word reach command exactly as you type am, so command receive the characters *, ., t, m and p, then e try treat dem as filename.

printf '%s\n' *.tmp

That one no cause problem with printf. But e cause problem with command wey dey create things. touch *.tmp for directory wey no get .tmp files go create file wey name na exactly *.tmp. To delete am later, you need quote am so shell no expand am again: rm -- '*.tmp'.

Two shell options dey change this rule. Try each one with pattern wey no match anything, then unset am.

shopt -s nullglob
printf '%s\n' *.tmp
shopt -u nullglob

shopt -s failglob
printf '%s\n' *.tmp
shopt -u failglob

nullglob make pattern wey no match expand to nothing, so command go run with fewer arguments than wetin you write. Na this one you want around loop. When nullglob dey off, for f in *.tmp go run the body once, with f holding the literal pattern. For almost every script wey do this, na bug. When nullglob dey on, the body go run zero times.

nullglob get im own danger, because command wey lose all im arguments still dey run. ls *.nope go turn to bare ls, wey go list the whole directory. grep -l needle *.nope go turn to grep -l needle, wey no get file to read, so e go wait for standard input and look like say e freeze. Turn nullglob on around the loop wey need am, then turn am off again after.

failglob use the other method: pattern wey no match na error, bash go report am, and command no go run at all. This setting good for interactive shell, because e stop mistyped pattern from reach rm as literal name.

Hidden files: dotglob

shopt -s dotglob
printf '%s\n' *
shopt -u dotglob

Run that block and compare the listing with the plain printf '%s\n' * from earlier. dotglob dey remove the leading dot rule, so * dey match hidden names too. E no allow * reach . or .., because even when dotglob dey on, na only pattern wey itself start with dot fit match those two names.

Without dotglob, .* na the normal way to reach hidden files, and e used to dey dangerous. For bash before 5.2, .* still dey match . and .., whether dotglob dey set or not. So recursive command like chmod -R 755 .* fit enter the parent directory directly. Bash 5.2 add globskipdots option, wey dey enabled by default, and e keep . and .. out of every expansion. Run shopt globskipdots before you depend on am, because older box no go get am, and newer one fit don turn am off.

globstar: match all the way down the tree

shopt -s globstar
printf '%s\n' **/*.log
printf '%s\n' **/
shopt -u globstar

When globstar dey set, any ** wey form one complete path component fit match files and directories for any depth, including zero level down. So **/*.log cover the current directory, plus logs/ and everything wey dey under archive/. A **/ wey get trailing slash dey match directories only. This make am quick way to see how tree take arrange.

If globstar no dey set, ** go behave like ordinary * and remain inside one directory. Run the same pattern when the option dey off and compare the result. If recursive pattern wey you copy from somewhere else return much less than you expect, na usually this be the reason. globstar dey off by default for every bash.

One limit wey you need know: ** no dey follow symbolic links to directories. If symlinks join the tree together, use find -L instead.

nocaseglob, for names wey case no agree

shopt -s nocaseglob
printf '%s\n' *.log
shopt -u nocaseglob

nocaseglob dey make filename matching ignore case, so uppercase .LOG suffix fit match lowercase .log pattern. Compare this listing with the one from the first section. Turn the option off again immediately, because if you leave am on, e go change matching for every command wey follow for that shell. Later, this fit make debugging confusing.

nocaseglob dey apply only to filename expansion. The related option for pattern matching inside a case statement or a double bracket test na nocasematch, and you set the two separately.

extglob: patterns wey fit talk say “no be dis one”

Extended patterns dey off by default. Turn am on for line wey stand alone:

shopt -s extglob

Five forms dey, and each one dey take list of patterns wey | separate:

  • ?(list) match zero or one occurrence.
  • *(list) match zero or more.
  • +(list) match one or more.
  • @(list) match exactly one of the alternatives.
  • !(list) match anything wey no match any alternative.

Shell wey already get the option on must read extended pattern. So, the batch below dey inside small script wey start with bash -O extglob. The -O flag set shopt option before the new shell read one line from the file. This make every pattern parse correctly. The last paragraph for this section explain why ordering matter.

cat > patterns.sh <<'EOF'
printf '%s\n' @(app|error).log
printf '%s\n' app.log.+([0-9])
printf '%s\n' *.@(md|txt)
printf '%s\n' !(*.log)
printf '%s\n' !(*.log|*.md|README)
EOF
bash -O extglob patterns.sh

+([0-9]) mean one or more digits, so e cover numbered suffix of any length. Na this fix the ? limit from earlier. E good make you run both patterns one after another so you fit see the difference.

The negation form answer the question “everything except”. Na the main reason to enable extglob. Two things about am dey surprise people.

E match directories and files. So, ls !(*.log) go list the contents of any directory inside the result instead of showing the directory name. Use ls -d, or the printf preview, to see the names themselves.

E also follow dot rule like every other glob. So, !(*.log) cover every visible name wey no end with .log. Hidden names still need dotglob set.

The ! inside !(...) na glob negation. E no get connection with history expansion and the bang character, wey be separate feature and dey run for another stage.

One more trap dey, and e happen during parse time. Bash parse complete command before e run any part of am. So, enabling extglob and using extended pattern for the same line go fail: pattern go parse while the option still dey off. Keep shopt -s extglob for line wey stand alone, before the patterns, and near the top of the script. Bash parse function body when e define the function, so the option must dey on before the definition, no be before the call. Starting shell with bash -O extglob, like the batch above do, settle the ordering before any parsing start.

Shell dey expand the pattern, command no dey do am

This na the second way e fit fail, and e explain one whole group of bugs.

grep -l needle *.log
find . -name '*.log'
find . -name *.log

The first line allow bash do the matching, and grep receive list of filenames. The second line quote the pattern, so find receive the five characters *.log and e do the matching by itself, for every depth below the starting point. The third line na the bug: bash first expand the pattern against current directory only, so find dem tell make e look for one specific name. For directory wey get one match, e silently search for the wrong thing. If matches dey two or more, find report usage error, because the extra names enter the place wey e expect expression.

The rule short. If command suppose handle the pattern, quote am. If shell suppose handle am, leave am bare. The same separation apply to --exclude patterns for tar and rsync, and --include filters for grep.

Globs no be regular expressions too, even though dem share some characters. For glob, * mean any run of characters. For regular expression, * mean zero or more of whatever come before am, so grep '*.log' dey ask for something wey different well-well from how e look.

Variables follow the same order of operations. Globbing run after variable expansion, so if variable no get quote and e hold pattern, shell go expand am against the directory.

pat='*.log'
printf '%s\n' $pat
printf '%s\n' "$pat"

If you no quote am, bash go glob the variable result. If you quote am, the pattern remain literal. Another related result wey you fit depend on be say: words wey globbing produce no dey split again for spaces, so for f in *.log handle filename wey get space as one name, while for f in $(ls *.log) break that name into two words. Loop directly over the glob.

Preview destructive pattern before you run am

Make you never use rm as the first command to test new pattern. Run the pattern with something wey no fit cause harm, read the list, then change only the command for the beginning of the line.

target='!(*.log)'
printf '%s\n' $target
ls -ld -- $target
rm -- $target

Writing the pattern inside target one time stop the three lines from becoming different, and $target no get quote on purpose, because na that one make bash expand am. printf '%s\n' print one name for each line and e no touch anything. ls -ld show each entry by itself instead of listing directory contents. The mode column show which entries be directories, and na that one you need check before any recursive delete. How to read the drwxr-xr-x permission bits explain that column.

Use the up arrow to bring back the previous line, then edit only the command, so the pattern remain exactly the same. If you type pattern again from memory, na there mistake fit enter.

Put -- before the pattern. E mark where options end, so filename wey start with - go count as name, no be flag.

If you want disable globbing for part of script, set -f disable am and set +f enable am again.

This habit important because rm no get undo. How to recover files deleted with rm -rf slow and e usually no complete, so preview cost much less than the other option.

When you finish, clean the practice directory. Print the path and read am first, because rm -rf wey point to wrong variable na exactly the accident wey this section dey warn about.

cd ~
echo "$lab"
rm -rf -- "$lab"

FAQ

find . -name *.log wrong files find ken?

Na bash expand *.log before find even start. Shell go match the pattern against current directory, then pass the filename wey e get to find. find go then search for that one name at every level. If two or more files match, find go receive extra arguments for where e expect expression, and e go report usage error. Put quote for the pattern as find . -name '*.log' so find fit do the matching by itself, with its own rules and recursion.

Wetin e mean when my pattern dey show as literal text?

E mean say nothing match. By default, bash dey leave pattern wey no match untouched and pass the raw characters to the command. The command then treat dem as filename. Set shopt -s failglob to turn unmatched pattern into error wey go stop the command, or use shopt -s nullglob make e expand to nothing. Under nullglob, check say the command still make sense when e get no arguments at all, because ls *.nope go become bare ls and list the whole directory.

How I fit match every file except one pattern for bash?

Enable extended patterns with shopt -s extglob for line by itself, then use the negation form. !(*.log) go match names wey no end with .log, and !(*.log|*.md) go exclude both. Hidden files no go enter the result unless you set dotglob too. Directories dey included, so preview the pattern with printf '%s\n' !(*.log) before you give am to rm.

Why * dey skip hidden files?

You must match leading dot literally, so * no dey ever match name wey start with dot. Use .* to reach hidden names, or set shopt -s dotglob to include dem for every ordinary pattern. Even with dotglob, * no dey ever match . or .., because only pattern wey start with dot fit match those two names. Bash 5.2 and later still keep dem out of .* through globskipdots option, wey dey on by default.

I need globstar before ** go work?

Yes. Without shopt -s globstar, bash go treat ** as ordinary *. E go match inside one directory and stop there. When globstar dey on, ** wey form complete path component fit match for any depth. So **/*.log go reach current directory and every subdirectory under am, while **/ by itself go match directories only. Note say ** no dey follow symbolic links to directories, so use find -L for tree wey symlinks build.

Dis article help you?

#bash#globbing#shell#linux#scripting