Globbing with * and ?

Wildcards

Lesson 14 of 20 · 8 min · Module 4: Find and organize

Typing every file name is slow. Wildcards let one short pattern stand for many names.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Match many names with *
  • Match single characters with ?
  • Predict what a pattern will expand to

The idea

The shell expands wildcards before running the command. * matches any characters, so *.txt means every name ending in .txt. ? matches exactly one character. report-202?.txt matches report-2025.txt and report-2026.txt.

ls data/*.txt
ls logs/*.log

01The shell does the matching

When you type cp data/*.txt archive/, cp never sees the star. The shell replaces the pattern with the matching names first, then runs cp with that list.

02Check a pattern with ls

Before copying or deleting with a wildcard, run ls with the same pattern. Whatever ls shows is exactly what the other command will receive.

ls data/report-*
Output
data/report-2025.txt  data/report-2026.txt

03Be precise

*.txt is broad; report-*.txt is narrower; report-202?.txt is narrower still. The more precise the pattern, the safer the command.

Your turn

Create a folder archive in your home folder. Copy both data/report-*.txt files into it with one cp command that uses a wildcard. No other files should end up in archive.

What the checks look for

  • Copies both report files
  • Copies nothing else
  • Uses a wildcard with cp
Try it in the editor →

Take this with you

Wildcards are expanded by the shell, so preview them with ls.

Go a little further

Use ls logs/*.log and ls logs/*/*.log to see the difference between one and two folder levels.

Learn Linux Command Line for free

20 short lessons with real code and instant checks.

See the whole course →