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
rm
command can be used to remove multiple files and directories (remember to use the option-r
to remove directories usingrm
). - The
ls
command 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:
in
in the fileinput.txt
out
in 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:
1
in the filefile1.txt
2
in the filefile2.txt
3
in 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
In the example above, the set [12]
matched:
1
in the filefile1.txt
2
in 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
In the example above, the set [!12]
matched:
3
in 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
In the example above, the class [[:digit:]]
matched:
1
in the filefile1.txt
2
in the filefile2.txt
3
in the filefile3.txt
Final words
Multiple wildcards can be used in the same pattern.
For instance:
file*.*
file?.*