A wildcard is a character that matches other characters in a name. A path with a wildcard is a pattern.
* matches any characters: one, many, or none.
| Pattern | Matches | Does not match |
|---|---|---|
*.txt | a.txt, week-1.txt | c.log |
week-* | week-1.txt, week-12.txt | day-1.txt |
* | All names that do not start with . | Hidden names |
The shell makes the list
The shell changes the pattern into the names that match, before the program starts. The program gets only the names:
So a pattern works with every command that takes names: ls, cat, cp, mv, rm.
learner@tidepool:~/shop$ mv *.txt ordersThis moves each file that ends with .txt into orders, with one line.
Look before you act
echo writes its arguments. Give it the pattern, and it writes the list that the shell made:
learner@tidepool:~/shop$ echo *.tmp
draft.tmp old.tmpThese are the names that rm *.tmp will delete. Use echo or ls with the same pattern before each rm.
When nothing matches
If no name matches, the shell gives the pattern to the program as it is:
learner@tidepool:~/shop$ rm *.log
rm: cannot remove '*.log': No such file or directoryHidden names
* does not match a . at the start of a name. *.txt does not match .secret.txt.