In Linux, a “wildcard” means that it could be a symbol or a group of symbols that stand for other characters. It is most often used to replace a string or a character. In Linux, wildcards are mostly used to make searches faster and more flexible. Most of the time, they are used in shell commands to run the commands that show the result. Most of the time, they are also used in regular expressions to match patterns in the system.
The following is a list of the fundamental wildcards available in Linux:
Symbol | Use |
---|---|
* | This wildcard can stand for any unknown number of characters. This is helpful when you want to find a file or document but can only remember part of its name. |
? | This wildcard only stands for one character that is not known. This is helpful if you have a list of files with similar names but are not sure about a few of the letters. |
[ ] | This wildcard is used to match any of the characters defined inside the square bracket, not the curly bracket. As we will see later, this option is helpful when working with both uppercase and lowercase files. |
How to use wildcards to match filenames
- Following command matches all file names that begin with the letter “e” and finish with any character.
ls e*
- Following command will match any files beginning with “e”, ending with “b”, and including any single character in between.
ls e?b.txt
- Following command matches all files beginning with the letter “e” and ending with the letter “b”, with a single character in between “[acd]” from the list of allowed characters enclosed in square brackets.
ls e[acd]b.txt
How to combined wildcards to match filenames
- We can use wildcards with ranges of numbers to find strings that match a pattern. Here, the result should start with “e” then match any characters, then match a range of numbers from 0 to 8, then match one character.