Working With Glob Patterns
Introduction: Many Files
Most commands so far have named files individually, as in cat notes.txt and head -n 3 access.log; we have also passed several filenames to cat. Glob patterns let us describe a filename set without typing every name, which helps when a folder contains many files.
This lesson introduces glob patterns: special characters that let you describe a whole set of filenames in one shot, instead of listing them one by one. You'll learn three pattern characters, discover who actually does the matching (it's not the command you'd expect), and pick up a safety habit for using patterns responsibly. By the end, you'll be able to name — and act on — an entire set of files with a single line.
Matching with *
Within one path component, the * wildcard matches zero or more characters. It does not cross /, and with zsh's default settings it does not select names beginning with a dot unless the pattern explicitly includes that dot. A pattern such as *.txt therefore selects the ordinary text filenames in the current folder.
A practice folder has already been prepared for you at ~/Desktop/Projects/globbing. It holds seven files, plus an empty archive subfolder we'll use later:
report1.txt,report2.txt,report3.txt,report9.txt— four files following the samereportN.txtshapesummary.txt— a fifth text file with a completely different nameapp.loganddb.log— two log files
Let's move into that folder:
Now let's ask for every .txt file at once:
All five .txt files printed, even though they don't share a common start — report1 and summary look nothing alike, but *.txt doesn't care what comes before .txt, only that something does. That's * doing exactly what it's built for: matching "anything" so you don't have to spell out every filename yourself.
Matching with ?
Where * is loose and matches any number of characters, ? is strict — it matches exactly one character, no more and no less. This precision matters when you want to be specific about a pattern's shape. If you used * where you meant ?, you might accidentally sweep up files you didn't intend to include, like report12.txt alongside report1.txt. ? protects you from that by locking the pattern to one exact slot.
Let's try it on our report files:
Notice that summary.txt didn't show up this time — it doesn't start with "report" at all, so it never had a chance to match. More importantly, if a file named report12.txt existed in this folder, report?.txt would skip it, too, because ? only ever fills in for one character, and 12 is two. *.txt would have caught a file like that; report?.txt deliberately would not.
