Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do wildcards match several filenames in a command-line argument?

Quick answer

Wildcards are pattern characters, commonly * for zero or more characters and ? for a character position. Their meaning depends on the shell and sometimes on the program receiving the argument. In Bash, unquoted wildcards are usually expanded by the shell before a command runs; in PowerShell, wildcard support depends on the command parameter. First list the matches in your own shell, check the results, and only then use the pattern elsewhere.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have several filenames with the same ending. What does a wildcard do, and how can I check what it will match before using it?

Geez

A wildcard is a pattern that can stand for part of a filename. Common patterns include *, for zero or more characters, and ?, for a character position. For example, report-*.txt describes names beginning with report- and ending with .txt. The exact matching behavior depends on the shell or command, so check it where you plan to use it.

In Bash, an unquoted *, ?, or [ can trigger filename expansion: Bash replaces the pattern with a sorted list of matching names before starting the command. By default, a pattern with no matches remains unchanged, though shell options can change that. Names beginning with . generally need a pattern that explicitly starts with a dot, unless a setting changes the behavior. [S1]

To inspect matches in Bash without opening or changing files, try:

printf '%s\n' report-*.txt

Read the output and confirm it shows the intended filenames. If there are no matches under Bash’s default behavior, the pattern itself may be printed.

Follow-up question

Why do people say the shell expands a wildcard? Does the command ever do the matching instead?

Geez

In the Bash example, the shell expands the pattern first, so the program receives the matching filenames rather than the original wildcard. Quoting it, as in printf '%s\n' 'report-*.txt', prevents Bash from expanding it; printf then prints the literal pattern.

PowerShell has its own wildcard behavior. Its patterns include *, ?, and bracket expressions, but support depends on the operator or command parameter. The documentation gives Get-ChildItem as an example of a command that accepts filename patterns; not every parameter accepts them. [S2] You can inspect a matching list with:

Get-ChildItem -Path 'report-*.txt'

Bash and PowerShell patterns should not be assumed interchangeable. A program may also define its own pattern rules, so check the relevant command’s documentation or help.

Follow-up question

How do I check a pattern with several parts, or names that start with a dot?

Geez

In Bash, report-??.txt describes names with two character positions between report- and .txt. report-[ab].txt uses a bracket expression. These are useful patterns to inspect, but verify their behavior in the shell you are using. [S1]

For dot-prefixed names in Bash, try an explicit pattern such as .*.txt if that is what you intend to check. By default, a pattern generally needs an explicit leading dot to match names beginning with one. [S1]

Run the listing check and compare each printed name with your intended rule. If the output is surprising, stop and investigate rather than using the pattern in another command. The listing shows matches for that shell or command in that location at that time; it does not establish what another tool or a later run will match.

Follow-up question

What should I verify before putting the wildcard into a command that does something?

Geez

Check whether the specific command argument accepts wildcards and whether the shell or the command will interpret the pattern. In PowerShell, wildcard support is parameter-specific. For a filename containing literal wildcard characters, the documentation describes escaping them or using -LiteralPath to avoid treating them as a pattern. [S2] In Bash, quoting prevents shell expansion, but the receiving program may still interpret the quoted pattern itself.

Before using an action command, confirm the intended location and inspect the listing there. Make sure every listed name is intended; if the output is empty, unexpectedly broad, or includes an unwanted name, do not proceed until you understand why. A preview checks the matches, not what a later command will do with them.

A practical checklist

  • Identify your shell and the command that will receive the pattern.

  • Use a listing-only command to inspect matches in the intended location.

  • Check how the pattern handles dot-prefixed names and no matches.

  • Confirm that the specific command argument accepts wildcards.

  • Proceed only when every listed filename is intended.

A few more useful answers

Does `*` match hidden files?

In Bash, names beginning with . generally require a pattern with an explicit leading dot unless the dotglob setting is enabled. Do not assume this rule applies unchanged in another shell. [S1]

Will the same wildcard work in Bash and PowerShell?

Not necessarily. Bash commonly expands unquoted filename patterns before launching a command, while PowerShell wildcard support depends on the operator or command parameter. Check the shell and command you plan to use. [S1] [S2]

How can I pass a wildcard character literally in PowerShell?

PowerShell documentation describes escaping wildcard characters or using -LiteralPath for filenames that contain literal wildcard characters. Check that the command supports the option. [S2]

Sources

References used for this guide.

  1. [S1] GNU Bash Reference Manual: Filename Expansion
    gnu.org · Checked
  2. [S2] PowerShell 7.6: about_Wildcards
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work