Wildcard characters are used to define the pattern for searching or matching text on string data in the bash shell. It is also used to create regular expressions. They are the special characters used as part of glob patterns. To use such a wildcard successfully, it must be outside of quotes(“).

Types of Wildcard

The three main wildcard characters are,

  1. Asterisk (*) : It is used to search for particular character(s) for zero or more times. It matches any number of any characters, including the empty string.
  2. Question mark (?) : It matches any single character.
  3. Square brackets ([]) : They are used to match with the characters of a defined range or a group of characters.

Use of Asterisk

Below example shows how to search all files whose name start with certain character. In the below example ls command is used to find out the list of files and folders of the in a directory. ls file1* lists all the files which name start with file1.

root@tryit-direct:~# ls                                                                                                                                
file1.txt  file11.txt  file2.txt  file3.txt      

# List all the files which start with 'file1'                                                                                                      
root@tryit-direct:~# ls file1*                                                                                                                         
file1.txt  file11.txt                                                                                                                                                                                                                                                  

Use of Question mark

When you know the exact numbers of characters that you want to search then question mark (?) wildcard can be used. ls file?.txt matches all the file names which has one character at the place of ? as shown below.

root@tryit-direct:~# ls                                                                                                                                
file1.txt  file11.txt  file2.txt  file3.txt         

# List all the files which has any single character between 'e' and '.'                                                                                                   
root@tryit-direct:~# ls file?.txt                                                                                                                      
file1.txt  file2.txt  

Use of Square Brackets

Different range of characters or group of characters can be used within square brackets ([]) for searching files based on the range. The following command will search any file whose name contains any two digit within 0-1.

root@tryit-session:~#
root@tryit-direct:~# ls                                                                                                                                
file1.txt  file11.txt  file2.txt  file3.txt    

# List all the files which has two numeric digit between zero and one. 
# Also file name should start with 'file' and end with '.txt'                                                                                                        
root@tryit-direct:~# ls file[0-1][0-1].txt                                                                                                             
file11.txt   

Brace Expansion

Brace Expansion {..} is often used in conjunction with wildcard. It is used for text substitution before any other bash expansion. It is not part of the Pattern Matching in Bash shell. The results of each expanded string are not sorted; left to right order is preserved.

root@tryit-cunning:~# echo a{1,2,3}b                                                                                                                   
a1b a2b a3b