Wildcards are used in search terms, to represent one or more characters. In wildcards, we do have 3 operators, asterisk
, question mark
, and square brackets
.
The *
asterisk can represent any number of characters. To be more precise it can represent zero or more characters.
1
2
3
4
5
6
7
8
9
10
11
# To match anything starting with fi letters.
ls fi*
# To match any file with il in between the filename.
ls *il*
# To match any file ending with a txt file extension.
ls *.txt
# To match anything starting with x, or y character, with any file extension.
ls {x,y}*.*
The ?
question mark can represent any single character. This is not limited to alphanumeric characters.
1
2
3
4
5
# To match any file that has a single character as filename, and 2 characters as a file extension.
ls ?.??
# To match any file that has a single character within the file and ends with a txt file extension.
ls file?.txt
The []
Square brackets allow you to specify a range of characters or numbers. You will easily understand this If you know about regular expression.
1
2
3
4
5
6
7
8
9
10
11
# To match all lowercase letters.
ls [a-z]*.txt
# To match all lowercase and uppercase letters.
ls [a-zA-Z]*.txt
# To match any filename including lowercase, and digits.
ls [a-z0-9]*.txt
# To match any file including digits, uppercase, and lowercase.
ls *[0-9].txt
Bonus!!! You can exclude anything inside the square brackets, that is following an !
exclamation, which will exclude any matches.
1
2
3
# return all txt files except file3.txt
ls ./file[!3].txt # option 1
ls ./[!file3]*.txt # option 2 (recommended)