Wildcards
Using wildcards to specify groups of filenames matching a pattern.
What are wildcards?
Wildcards are special characters that match one or more characters in filenames.
| Wildcard | Meaning |
|---|---|
* | Matches any sequence of characters. |
? | Matches any single character. |
[characters] | Matches any single character in a set. |
[!characters] | Matches any single character not in a set. |
[[:class:]] | Matches any character in the specified class. |
Many commands accept groups of files and directories matched using wildcards.
For instance:
- The
rmcommand can be used to remove multiple files and directories (remember to use the option-rto remove directories usingrm). - The
lscommand can be used to list files and the contents of directories.
Examples
*
The symbol * (asterisk) matches any sequence of one or more characters.
For instance:
ls *put.txt

In the example above, the * symbol matched:
inin the fileinput.txtoutin the fileoutput.txt
?
The symbol ? (question mark) matches any single character at that position.
For instance:
ls file?.txt

In the example above, the ? symbol matched:
1in the filefile1.txt2in the filefile2.txt3in the filefile3.txt
[characters]
The syntax [ ] (square brackets) can be used to specify a set of characters
that can be matched at a given position.
Only one of the characters in the set can be matched at the given position, no matter how many characters are present in the set.
For instance:
ls file[12].txt
![Using the ‘[characters]’ wildcard.](https://obds-training.github.io/Help/docs/linux/essentials/wildcards/bracket-include_hub2b8fd9346a6bfd2fb19f0b10c978004_74557_20x0_resize_box_3.png)
In the example above, the set [12] matched:
1in the filefile1.txt2in the filefile2.txt
[!characters]
A set of characters prefixed with the symbol ! (exclamation mark)
can be used to specify a set of character that must not match
at the given position.
For instance:
ls file[!12].txt
![Using the ‘[!characters]’ wildcard.](https://obds-training.github.io/Help/docs/linux/essentials/wildcards/bracket-exclude_hu9c5ccd3f650c80552d8eaee8fc0d01bd_72875_20x0_resize_box_3.png)
In the example above, the set [!12] matched:
3in the filefile3.txt
[[:class:]]
The syntax [[: :]] (two square brackets and a colon symbol)
can be used to specify a class of characters that can be matched at a given position.
Commonly used classes of characters are listed below. Find more on the page Character Classes and Bracket Expressions →.
| Class | Description |
|---|---|
alnum | Alphanumeric characters, including alpha and digit. |
alpha | Alphabetic characters, including lower and upper. |
digit | Digits: 0 1 2 3 4 5 6 7 8 9. |
lower | Lower-case letters: a b c d e f g h i j k l m n o p q r s t u v w x y z. |
punct | Punctuation characters: ! " # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~. |
upper | Upper-case letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z. |
For instance:
ls file[[:digit:]].txt
![Using the ‘[[:class:]]’ wildcard.](https://obds-training.github.io/Help/docs/linux/essentials/wildcards/bracket-class_hu10eefdc0d4606c80eac1ca9b6ccb379e_75043_20x0_resize_box_3.png)
In the example above, the class [[:digit:]] matched:
1in the filefile1.txt2in the filefile2.txt3in the filefile3.txt
Final words
Multiple wildcards can be used in the same pattern.
For instance:
file*.*file?.*