Linux Wildcards: Matching File Names with ?, *, and []
Learn how Linux shell wildcards ?, *, and [] match filenames, how pathname expansion works, and how to use patterns safely with ls, cp, mv, and rm.
Linux wildcards let you describe filename patterns without typing every filename. They are special pattern characters interpreted by the shell, such as Bash, before a command runs. Wildcards are also called globs or filename expansion patterns.
For example, when you run ls O??d, the shell first finds filenames matching O??d. It then passes the matching names as arguments to ls. This shell process is called pathname expansion.
Common Linux wildcard symbols
| Pattern symbol | What it matches | Example pattern | Example matching filenames |
|---|---|---|---|
? | Exactly one character | O??d | Oind, Okhd, Oerd |
* | Zero, one, or many characters | O*d | Od, Oad, Oereed |
[characters] | Exactly one character from a listed set | O[ac]d | Oad, Ocd |
[start-end] | Exactly one character within a range | O[a-e]d | Oad, Obd, Ocd, Odd, Oed |
The single-character wildcard: ?
A question mark matches exactly one character in a filename. Each question mark represents one required position.
The pattern O??d means:
- The name starts with uppercase
O. - It has exactly two characters after
O. - It ends with lowercase
d.
Therefore, Oind, Okhd, and Oerd match. Oad is too short because it has only one middle character. Od has no middle characters, while Oereed and Oerererd have too many characters.
ls O??d
In the sample directory described below, this command selects:
Oerd Oind Okhd
The multi-character wildcard: *
An asterisk matches a sequence of zero, one, or many characters. Unlike ?, it does not require a fixed number of characters.
The pattern O*d matches any name that starts with O and ends with d. The middle portion can have any length, including length zero. Consequently, Od also matches.
ls O*d
For the sample files, O*d matches all of these names:
Oad Ocd Od Oerd Oerererd Oereed Oind Okhd
The asterisk can also appear at the beginning or end of a pattern. For example, *.log commonly matches names ending in .log, while backup* matches names beginning with backup. Filename matching is separate from regular expressions: shell * means any sequence of filename characters, while regular expression syntax uses different rules.
Bracket expressions: []
A bracket expression matches exactly one character selected from a list or range inside square brackets.
Character lists
O[ac]d matches names beginning with O, ending with d, and containing either lowercase a or lowercase c in the middle position.
ls O[ac]d
For the sample files, the result is:
Oad Ocd
It does not match Oind, because the middle portion is not one character, or OAd, because the uppercase A is not in the list.
Character ranges
A range such as [a-e] represents one character from the range beginning at a and ending at e. Thus, O[a-e]d can match Oad, Obd, Ocd, Odd, or Oed.
ls O[a-e]d
With only the sample files in this lesson, the visible matches are:
Oad Ocd
If the directory also contained Obd, Odd, and Oed, those names would match as well. Bracket expressions are case-sensitive in ordinary use: [a-e] does not normally include uppercase A through E. Locale settings can affect range ordering, so use an explicit list such as [abcde] when you need an exact set of characters.
Practice with a controlled file set
Create the example files in an empty practice directory. The touch command creates empty files if they do not already exist.
touch Oind Okhd Oerd Oereed Oad Ocd Oerererd Od
The directory now contains:
Oind Okhd Oerd Oereed Oad Ocd Oerererd Od
Pattern matching results
| Filename | Matches O??d | Matches O*d | Matches O[ac]d | Matches O[a-e]d |
|---|---|---|---|---|
Oind | Yes | Yes | No | No |
Okhd | Yes | Yes | No | No |
Oerd | Yes | Yes | No | No |
Oereed | No | Yes | No | No |
Oad | No | Yes | Yes | Yes |
Ocd | No | Yes | Yes | Yes |
Oerererd | No | Yes | No | No |
Od | No | Yes | No | No |
Using wildcard patterns with commands
A wildcard pattern is typed directly as a command argument. Do not add special syntax around it when you want the shell to expand it.
ls O??d
ls O*d
ls O[ac]d
ls O[a-e]d
The same expansion can be used with commands that accept filenames, including cp, mv, and rm. For example, cp O??d destination/ passes every filename matching O??d to cp. Since one pattern can expand to multiple names, always verify the result before modifying files.
Safe wildcard usage
Preview matches before changing files
Wildcards can affect multiple files at once. Before running rm, mv, cp, chmod, or another modifying command, preview the expansion.
ls -- O*d
printf '%s\n' O*d
rm -- O*d
Run the first or second command and inspect the names. Only run the deletion command when the complete match set is correct. The -- marks the end of command options, so a filename beginning with a hyphen is less likely to be interpreted as an option.
Quoting prevents normal expansion
Single and double quotes normally prevent wildcard characters from being expanded by the shell:
ls 'O*d'
ls "O*d"
These commands pass the literal text O*d to ls instead of passing the names matched by the pattern. If the pattern was quoted or escaped accidentally, remove the unnecessary quotes or backslash when pathname expansion is intended. Quoting is useful when you deliberately need literal wildcard text.
Unmatched patterns
If no filename matches a pattern, the result depends on the shell and its configuration. Some shells leave the pattern unchanged, some report an error, and some expand it to nothing. When a command reports that a literal wildcard filename does not exist, check whether the pattern actually matched anything and learn the unmatched-glob behavior of your current shell.
Troubleshooting wildcard patterns
- The wildcard appears literally: Check for quotes or backslashes, such as in
ls 'O*d'. - No files are listed: Verify spelling and capitalization. Check that the number of
?characters equals the required number of filename characters. - The pattern matches too many files: Remember that
*matches zero or many characters. Preview withlsorprintfbefore using a modifying command. - A bracket pattern gives unexpected results: Check the listed characters, range endpoints, capitalization, and locale behavior. Use an explicit list such as
[abcde]when necessary. - A command says a literal wildcard file does not exist: The shell may have left an unmatched pattern unchanged. Confirm the directory contents and shell behavior.
Exam-relevant notes
?matches exactly one character.*matches zero, one, or many characters.[abc]matches exactly one character from the listed set.[a-e]expresses a character range, subject to shell and locale rules.- The shell expands globs before commands such as
ls,cp,mv, andrmreceive their arguments. - Quoting a glob normally stops pathname expansion.
- Always preview broad or destructive matches.
For more shell background, see Bash and Linux command-line topics.