* matches any number of characters. Sometimes that is too much. Two more wildcards match exactly one character.
| Wildcard | Matches |
|---|---|
* | Any characters, also none |
? | Exactly one character |
[abc] | One character: a, b or c |
[0-9] | One character in the range: a digit from 0 to 9 |
? : exactly one character
week-?exactly one character.txt
| Name | week-?.txt | week-??.txt | week-*.txt |
|---|---|---|---|
week-1.txt | Yes | No | Yes |
week-9.txt | Yes | No | Yes |
week-12.txt | No | Yes | Yes |
[ ]: one character from a set
Put the characters that you allow between [ and ]:
text
learner@tidepool:~/shop$ ls shift-[ab].txt
shift-a.txt shift-b.txtA - between two characters makes a range:
| Set | Matches one of |
|---|---|
[0-9] | 0 1 2 … 9 |
[a-c] | a b c |
[13] | 1 3 |
[ ] always matches one character only. week-[0-9].txt does not match week-12.txt.
Put wildcards together
You can use more than one wildcard in a pattern. week-[0-9]?.txt matches a digit, then exactly one more character: week-10.txt to week-99.txt.
Use echo to check a pattern before you act on it, the same as with *.