Command Line File Globbing: Everything You Need to Know

This blog post will teach you everything you need to know about command line file globbing, including what it is, how to use it, and some common shortcuts and tips

File Globbing

  • Globbing is wild card expansion:
    • * – matches zero or more characters
    • ? – matches any single character
    • [0-9] – matches a range of numbers
    • [abc] – matches any one of the characters in the list
    • [^abc] – matches any one of the characters in the list
    • [:alpha:] – characters in a predefined character class can be matched
  • glob(7)

When typing commands, it is often useful to issue the same command on more than one file at the same time. The use of wild cards, or metacharacters, allows one pattern to be expanded into multiple file names by a process called globbing. For example, if a directory contains the following files: joshua.txt James.txt alex.txt Angelo.txt gonk.mp3 zonk.mp3

Typing the following command:
[amar@amar-pc ~ ]$ rm *mp3

will have the same results as typing:
[amar@amar-pc ~ ]$ rm gonk.mp3 zonk.mp3

echo can be used to test metacharacter-expansion before executing a destructive command like rm:

[amar@amar-pc ~ ]$ echo ?o*
joshua.txt gonk.mp3 zonk.mp3

Use of character collation ranges (such as [a-z] is possible but considered hazardous. They are not portable since they may sort differently in different languages and locales and may result in unexpected results after expansion.

Instead, it is better to use character lists ([abcde]) or POSIX character classes ([:lower:]). Character classes can also be used for matching in regular expressions used by other applications, they are not limited to the bash shell.

The syntax for a character class is [:keyword:], where some possible choices for keyword include:
alpha, upper, lower, digit, alnum, punct, space.

For example, if you want to match any one number, you might use:

If you want to match any one non-alphanumeric character:


For more information see the glob(7) man page (using the command man 7 glob).

Leave a Comment

%d bloggers like this: