AI Hints
Home›Linux Tutorial›Folders and Wildcards
Linux · Lesson 29 of 120

Wildcards

Many files at once.

⏱ 2 min read▶ Video at 35:41🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Imagine your downloads folder has fifty photos, and you want to move them all. Typing fifty names would take forever. Wildcards let you describe many files with one short pattern.

Wildcards
From the video: Wildcards
  • Fifty photos to move
  • Typing every name? No
  • One pattern for many files

The star

Here's a messy downloads folder. The star matches any characters, any number of them. So star dot JPG means, every name that ends in dot JPG. And invoice star means every name that starts with invoice.

Terminal
ls
invoice-feb.pdf  notes1.txt   notes2.txt  photo2.jpg  setup.deb
invoice-jan.pdf  notes10.txt  photo1.jpg  photo3.jpg  song.mp3
ls *.jpg
photo1.jpg  photo2.jpg  photo3.jpg
ls invoice*
invoice-feb.pdf  invoice-jan.pdf

The question mark

A question mark matches exactly one character. Notes question mark dot TXT matches notes one, and notes two, but not notes ten, because ten has two characters there. Square brackets match one character from a list, like photo, one or three.

Terminal
ls notes?.txt
notes1.txt  notes2.txt
ls photo[13].jpg
photo1.jpg  photo3.jpg

How wildcards really work

Here's the secret. The shell expands the wildcard before the command even runs. So LS never sees the star. It receives the full list of matching names. That's why wildcards work with every command. CP, MV, RM, all of them. And it's why you should always check a pattern with LS, before using it with RM.

keyvalue
The shellexpands the pattern
The commandgets the full list
Works withcp, mv, rm, anything
Tiptest with ls first

Summary

The star matches anything, the question mark matches one character, and brackets match a list. Always test a pattern with LS first. Next, let's tidy that downloads folder.

Key takeaways
  • * any characters
  • ? one character
  • [13] one of a list
Next up: Practice: Tidy Downloads — wildcards for real