Can you combine `*` and `?` in a pattern?

QuestionsQuestions8 SkillsProDec, 18 2025
093

Yes, absolutely! You can combine * and ? (as well as square brackets []) in a single pattern to create very specific searches.

When you combine them, the shell processes each metacharacter according to its own rule:

  • ? will match exactly one character at its specific position.
  • * will match any number of characters (including zero) for the rest of that part of the pattern.

Example Scenario

Imagine you have the following files:

  • test1.txt
  • test2.txt
  • test10.txt
  • taskA.txt
  • taskAB.txt

If you use the pattern t?st*.txt:

  1. t: Matches the literal letter 't'.
  2. ?: Matches exactly one character (like 'e' in test or 'a' in task).
  3. st: Matches the literal letters 'st'.
  4. *: Matches any characters remaining before the extension (like '1', '2', or '10').
  5. .txt: Matches the literal extension.

Results of ls t?st*.txt:

  • It would match test1.txt, test2.txt, and test10.txt.
  • It would not match taskA.txt because the pattern expects st after the second character.

Try it in your VM!

Since you just learned about touch, you can test this combination yourself:

# Create some test files
touch match1.ox match22.ox matchABC.ox mismatch.ox

# Try a combined pattern
# This looks for "match", then exactly ONE character, then anything else ending in .ox
ls match?*.ox

In this case, match?*.ox would find match1.ox, match22.ox, and matchABC.ox because they all have at least one character after the word "match".

Do you have a specific naming pattern in mind that you're trying to match?

0 Comments

no data
Be the first to share your comment!