?, *, and escape sequences to match complex filename patterns. Glob patterns allow you to filter filenames efficiently without resorting to regular expressions.
For an in-depth reference on Bash filename expansion, see Bash Pattern Matching.
| Wildcard | Description | Example |
|---|---|---|
* | Matches zero or more characters | *.txt |
? | Matches exactly one character | file.? |
\ | Escapes the next character literally | file\*.* |
Workflow for Building Complex Globs
-
Sample Selection
Collect filenames and mark matches (✅) vs. non-matches (❌). Identify blue segments (literal text) and yellow segments (wildcards). -
Category Order
Note the sequence of blue and yellow segments in the target filenames. -
Construct the Glob
Concatenate literals and wildcards in the order you determined.
Example 1: Match “file.” plus at Least One Character
Samples- Blue:
file. - Yellow:
?(one char) then*(rest) - Glob:
file.?*
Example 2: Match “file_” Prefix and a Three-Character Extension
Samples- Blue:
file_ - Yellow:
*(any chars before dot) - Blue:
. - Yellow:
???(exactly three) - Glob:
file_*.___
Example 3: Match Any Name with a Four-Character Extension
Samples- Yellow:
*(any prefix) - Blue:
. - Yellow:
????(exactly four) - Glob:
*.????
Example 4: Match Names Containing “1” Then One More Character, Then an Extension
Samples- Yellow:
*(any prefix) - Blue:
1 - Yellow:
?(one char) - Blue:
. - Yellow:
*(any extension) - Glob:
*1?.*
Example 5: Escaping a Literal “*” in Filenames
Samples- Yellow:
?(any single) - Blue:
\*(escaped*) - Blue:
a - Yellow:
* - Blue:
. - Yellow:
* - Glob:
?\*a*.*
Be sure to quote or escape the pattern in your shell to prevent expansion before
ls sees it.Example 6: Escaping a Backslash and Question Mark
Samples- Yellow:
*(any prefix) - Blue:
\\?(escaped\?) - Yellow:
*(any remainder) - Glob:
*\\?*