* matches any string and ? matches a single character, square brackets ([ ]) define character classes, enabling you to match one character from a specific set or range.
Matching a Range of Characters
Given a directory:fileA, fileB, and fileC by specifying a range inside brackets:
[A-C] matches any uppercase letter from A through C. Note that the dash (-) defines a range and must go from lower to higher:
Common Examples
| Pattern | Matches | Description |
|---|---|---|
| file[A-C] | fileA, fileB, fileC | Uppercase A–C |
| file[a-c] | filea, fileb, filec | Lowercase a–c |
| file[1-3] | file1, file2, file3 | Numeric 1–3 |
| file[ACE] | fileA, fileC, fileE | Specific letters A, C, E |
Negated Character Classes
Prefix! or ^ inside brackets to exclude characters or ranges:
In Bash both
! and ^ work for negation. POSIX shells require ! at the start of the class.
Listing Specific Characters
To match non-consecutive filenames such asfileA, fileC, and fileE, simply list them:
Case Sensitivity
Globbing in Bash is case sensitive. If you have both uppercase and lowercase files:a–e, combine ranges:
When mixing ranges, list them in the order you want matched: here
a-e before A-E.Numeric Ranges and Negation
Numeric ranges behave the same way:Multiple Character Classes
You can chain classes to match multiple positions. For example, to matchfilea1, filea2, fileb1, fileb2:
[a-b] matches one letter, and [1-2] matches one digit. If you try only [1-2], it won’t match because the letter is missing:
Literal Characters Inside Brackets
Inside character classes, special glob characters lose their meaning:* as a wildcard, place it outside the brackets:
Globbing vs. File Creation
Globs match existing filenames; they do not generate names. If you use a glob in a command liketouch when no files match, the pattern is taken literally:
Globbing won’t create files—only match them. If you expect new files, use brace expansion or a loop.