Skip to main content
Shell globbing lets you match filenames using patterns. While * 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:
You can match only fileA, fileB, and fileC by specifying a range inside brackets:
Here, [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

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.
The image explains that square brackets are special characters in Shell used for creating glob expressions by matching characters inside the brackets.

Listing Specific Characters

To match non-consecutive filenames such as fileA, fileC, and fileE, simply list them:

Case Sensitivity

Globbing in Bash is case sensitive. If you have both uppercase and lowercase files:
To match only lowercase:
To remove both lowercase and uppercase 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 match filea1, filea2, fileb1, fileb2:
Each [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:
To use * as a wildcard, place it outside the brackets:
Or constrain both positions:

Globbing vs. File Creation

Globs match existing filenames; they do not generate names. If you use a glob in a command like touch when no files match, the pattern is taken literally:
To produce a series of filenames based on a pattern, consider using brace expansion instead of globs.
Globbing won’t create files—only match them. If you expect new files, use brace expansion or a loop.

Watch Video

Practice Lab