Wildcards: work with many files at once
Use the * ? and [ ] wildcards to pick many files at once, and see how the shell expands them before a command runs. Practise in your browser.
Typing ten file names is slow. Wildcards let one pattern stand for many names:
| Pattern | Matches |
|---|---|
* | any characters, any number of them (even none) |
? | exactly one character |
[abc] | one of the characters inside the brackets |
[0-9] | one character in that range |
So *.pdf is every name ending in .pdf, report-202?.pdf matches report-2025.pdf and report-2026.pdf, and IMG_010[12].jpg matches just the first two photos.
The shell does the matching
The command never sees the *. Before it runs, the shell swaps the pattern for the matching names, sorted. echo makes this visible:
echo downloads/*.pdfls downloads/IMG_*Two things follow from that:
- A pattern is matched against the directory it names.
*.jpglooks where you are;downloads/*.jpglooks in downloads. - If nothing matches, the pattern is passed on unchanged, and the command complains that a file called
*.jpgdoesn't exist.
Wildcards skip hidden files, and they're case-sensitive: *.jpg won't match HOLIDAY.JPG.
Tip:Before using a pattern with rm or mv, try it with ls or echo to see exactly which names it means.
Your turn
Move every .jpg file from downloads into photos, leaving all the other files in downloads.
Your task
Move every .jpg file from downloads into photos, leaving all the other files in downloads.
- All four .jpg files are in photos (not done yet)
- Nothing else moved (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
*- Wildcard: any characters, any number of them (even none)
?- Wildcard: exactly one character
[ ]- Wildcard: one character from the set, such as [abc] or [0-9]