Skip to content

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.

Lesson 9 of 166 minSkip to the terminal ↓

Typing ten file names is slow. Wildcards let one pattern stand for many names:

PatternMatches
*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/*.pdf
ls downloads/IMG_*

Two things follow from that:

  • A pattern is matched against the directory it names. *.jpg looks where you are; downloads/*.jpg looks in downloads.
  • If nothing matches, the pattern is passed on unchanged, and the command complains that a file called *.jpg doesn't exist.

Wildcards skip hidden files, and they're case-sensitive: *.jpg won't match HOLIDAY.JPG.

Before using a pattern with rm or mv, try it with ls or echo to see exactly which names it means.

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)

Lesson 9 of 16.

learner@sandbox: ~bash (practice)
Note: Practice shell for this lesson. Type a command and press Enter (help lists them all).