How to match multiple characters with wildcards?

QuestionsQuestions8 SkillsProDec, 23 2025
0134

To match multiple characters at once with wildcards, you primarily use the asterisk symbol: *.

1. The * Wildcard (The most common way)

The * matches any number of characters (including zero characters). It is your go-to tool when you don't know the exact length or content of a part of a filename.

  • Example: ls *.txt
    • Matches: a.txt, hello.txt, my_very_long_filename.txt
  • Example: ls note*
    • Matches: note, note1, note_backup_2023, notes.txt

2. Using multiple ? Wildcards

If you want to match a specific number of characters, you can use the ? symbol multiple times. Each ? represents exactly one character.

  • Example: ls ???.txt
    • Matches: cat.txt, dog.txt, 123.txt
    • Does NOT match: file.txt (4 characters) or me.txt (2 characters).

3. Combining Wildcards

You can combine different wildcards to be very specific:

  • Example: ls f*[1-3].*
    • This looks for files starting with f, ending with a number between 1 and 3, followed by a dot and any extension.
    • In your current lab, this would match file1.txt, file2.txt, and file3.txt.

4. Brace Expansion (Bonus)

In your lab, you used {1..5}. While technically called brace expansion rather than a wildcard, it’s a powerful way to generate or match multiple specific patterns at once.

  • Example: ls {file,note}_1.txt
    • Matches: file_1.txt and note_1.txt.

Summary:

  • Use * for "as many characters as there are."
  • Use ??? for a specific number of characters.

Give ls f*.txt a try in your terminal to see it in action with the files you just created!

0 Comments

no data
Be the first to share your comment!