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.

WildcardMeaning
*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 using rm).
  • 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

Using the ‘*’ wildcard.

In the example above, the * symbol matched:

  • in in the file input.txt
  • out in the file output.txt

?

The symbol ? (question mark) matches any single character at that position.

For instance:

ls file?.txt

Using the ‘?’ wildcard.

In the example above, the ? symbol matched:

  • 1 in the file file1.txt
  • 2 in the file file2.txt
  • 3 in the file file3.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.

In the example above, the set [12] matched:

  • 1 in the file file1.txt
  • 2 in the file file2.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.

In the example above, the set [!12] matched:

  • 3 in the file file3.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 →.

ClassDescription
alnumAlphanumeric characters, including alpha and digit.
alphaAlphabetic characters, including lower and upper.
digitDigits: 0 1 2 3 4 5 6 7 8 9.
lowerLower-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.
punctPunctuation characters: ! " # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.
upperUpper-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 &lsquo;[[:class:]]&rsquo; wildcard.

In the example above, the class [[:digit:]] matched:

  • 1 in the file file1.txt
  • 2 in the file file2.txt
  • 3 in the file file3.txt

Final words

Multiple wildcards can be used in the same pattern.

For instance:

  • file*.*
  • file?.*