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
- Matches:
- Example:
ls note*- Matches:
note,note1,note_backup_2023,notes.txt
- Matches:
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) orme.txt(2 characters).
- Matches:
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 between1and3, followed by a dot and any extension. - In your current lab, this would match
file1.txt,file2.txt, andfile3.txt.
- This looks for files starting with
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.txtandnote_1.txt.
- Matches:
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!