Skip to main content

How to Match Many Files with a Wildcard in the Terminal

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Typing every file's name gets old fast. The * wildcard stands for any part of a name, so one short pattern such as reports/*/summary.txt names every matching file for you. zsh, bash and PowerShell all understand it, with one difference that matters: what happens when nothing matches.

Syntax​

A wildcard goes where a file or folder name would go, in any command that takes paths:

Syntax
<folder>/*<rest of name>
<folder>/*/<name>
PartWhat it does
*Stands for any characters, including none, within one name. *.txt is every name that ends in .txt.
<folder>/*/<name>* between two slashes stands for any one folder, so reports/*/summary.txt is the summary.txt in each folder inside reports.
/Never matched by *. A pattern reaches exactly as many folders deep as it has slashes.

Who reads the pattern differs:

  • zsh and bash replace the pattern with the matching names, sorted, before the command starts. The command never sees the *. The zsh manual puts it this way: "The word is replaced with a list of sorted filenames that match the pattern."
  • PowerShell passes the pattern to the command, and a parameter that accepts wildcards does the matching. Microsoft's reference says which parameters do: Remove-Item's Path, for example, says "Wildcard characters are permitted."

The Example​

The example has a reports folder with one folder per month, two summary.txt files and a notes.md:

reports
reports/2026-10/summary.txt
reports/2026-11/notes.md
reports/2026-11/summary.txt

It shows what three patterns match, what happens when a pattern matches nothing, and then deletes both summaries with one command.

Step 1: Create the Example Files​

Terminal
mkdir -p reports/2026-10 reports/2026-11
touch reports/2026-10/summary.txt reports/2026-11/summary.txt reports/2026-11/notes.md

Step 2: See What a Pattern Matches​

Before a pattern goes into a command that changes files, look at what it names:

Terminal
echo reports/*/summary.txt
Output
reports/2026-10/summary.txt reports/2026-11/summary.txt

echo prints its arguments, so it shows exactly what the shell put in place of the pattern: both summaries, and not notes.md.

Step 3: Match Everything in One Folder​

Terminal
echo reports/2026-11/*
echo reports/*
Output
reports/2026-11/notes.md reports/2026-11/summary.txt
reports/2026-10 reports/2026-11

reports/2026-11/* is everything in that one folder. reports/* is only the two month folders, not the files inside them, because * doesn't match /.

Step 4: See What Happens When Nothing Matches​

The folder has no CSV files:

In zsh, the default shell on macOS:

Terminal
echo reports/*.csv
Output
zsh: no matches found: reports/*.csv

zsh stops the whole command and runs nothing. bash leaves the pattern as it is instead and runs the command with it, so the command gets the name reports/*.csv. Here is bash 3.2 on the same Mac:

Output in bash
$ echo reports/*.csv
reports/*.csv
$ rm reports/*.csv
rm: reports/*.csv: No such file or directory

Both shells' manuals describe this. zsh: "If no matching pattern is found, the shell gives an error message". bash: "If no matching file names are found, and the shell option nullglob is disabled, the word is left unchanged."

Step 5: Use the Pattern in a Command​

Delete both summaries in one command, then look at what's left:

Terminal
rm reports/*/summary.txt
echo reports/*/*
Output
reports/2026-11/notes.md

rm received the two paths that Step 2 showed and deleted both. reports/*/*, every file one folder down, now finds only notes.md.

A macOS-style terminal window with two commented commands: echo reports//summary.txt to see which files the pattern matches, and rm reports//summary.txt to delete them.

Common Mistakes​

MistakeWhat happensDo this instead
Deleting with a pattern you haven't looked atEvery file it matches goes, including ones you didn't think ofRun it with echo or Resolve-Path first, as in Step 2
Expecting reports/* to reach files in subfoldersIt matches only the names directly inside reportsAdd one */ for each folder level
Quoting the pattern: echo 'reports/*/summary.txt'Prints reports/*/summary.txt as typed: quotes stop the shell from matchingLeave a pattern you want matched unquoted

Troubleshooting​

SymptomLikely causeFix
zsh: no matches found: …Nothing matches the patternCheck the folder names and the pattern with echo
rm: reports/*.csv: No such file or directory in bashNothing matched, so bash passed the pattern itself to rmSame as above
A PowerShell command does nothingThe pattern matched nothing, which PowerShell treats as an empty resultRun Resolve-Path <pattern> -Relative to see what it matches

FAQ​

Does * match hidden files?​

Not in zsh and bash. Both manuals say a . at the start of a name must be matched explicitly, so * skips names such as .env.

Is the match case-sensitive?​

In PowerShell, no: Microsoft's wildcard article says that for parameters that accept wildcards, "their use is case-insensitive."

Are there other wildcards?​

Yes. ? stands for exactly one character in zsh and bash, and [...] for one of the characters in the brackets. This post covers *, the one you'll use most.

Conclusion​

* stands for any characters within one name, so reports/*/summary.txt names every summary.txt one folder down. zsh and bash replace the pattern with the matching names before the command runs, and PowerShell's commands do the matching themselves. Look at what a pattern matches before you delete with it, and remember that zsh refuses a pattern that matches nothing, bash passes it on unchanged, and PowerShell returns nothing.

Additional Resources​