Skip to main content
In this lesson, we’ll take your globbing skills to the next level by combining ?, *, 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.
Below is a quick summary of common glob operators:

Workflow for Building Complex Globs

  1. Sample Selection
    Collect filenames and mark matches (✅) vs. non-matches (❌). Identify blue segments (literal text) and yellow segments (wildcards).
  2. Category Order
    Note the sequence of blue and yellow segments in the target filenames.
  3. Construct the Glob
    Concatenate literals and wildcards in the order you determined.

Example 1: Match “file.” plus at Least One Character

Samples
  1. Blue: file.
  2. Yellow: ? (one char) then * (rest)
  3. Glob: file.?*

Example 2: Match “file_” Prefix and a Three-Character Extension

Samples
  1. Blue: file_
  2. Yellow: * (any chars before dot)
  3. Blue: .
  4. Yellow: ??? (exactly three)
  5. Glob: file_*.___

Example 3: Match Any Name with a Four-Character Extension

Samples
  1. Yellow: * (any prefix)
  2. Blue: .
  3. Yellow: ???? (exactly four)
  4. Glob: *.????

Example 4: Match Names Containing “1” Then One More Character, Then an Extension

Samples
  1. Yellow: * (any prefix)
  2. Blue: 1
  3. Yellow: ? (one char)
  4. Blue: .
  5. Yellow: * (any extension)
  6. Glob: *1?.*

Example 5: Escaping a Literal “*” in Filenames

Samples
  1. Yellow: ? (any single)
  2. Blue: \* (escaped *)
  3. Blue: a
  4. Yellow: *
  5. Blue: .
  6. Yellow: *
  7. 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
  1. Yellow: * (any prefix)
  2. Blue: \\? (escaped \?)
  3. Yellow: * (any remainder)
  4. Glob: *\\?*

Conclusion

By splitting filenames into literal (blue) and wildcard (yellow) segments, you can craft precise glob patterns for matching even the trickiest file names.

Watch Video