Wildcard characters:
The three main wildcard characters are,
- Star or Asterisk (*)
- Question mark (?)
- Square brackets ([])
Asterisk (*) is used to search for particular character(s) for zero or more times. Question mark (?) is used to search for a fixed number of characters where each question mark (?) indicates each character. Square brackets are used to match with the characters of a defined range or a group of characters. The uses of these characters are shown in the next part of this tutorial.
Use of asterisk (*)
Asterisk (*) can be used in various ways with shell commands for searching files. Different use of asterisk (*) are shown in the following examples.
Example – 1: Searching specific file with filename and ‘*’
‘ls’ command is used to find out the list of files and folders of the current directory. ‘ls s*’ command will search all files whose name start with ‘s’
$ ls s*
Example – 2: Searching file with particular extension and ‘*’
You can search any file by using asterisk (*) and the file extension. If you want to search all files with ‘.sh’ extension from the current directory then run the following command from the terminal. Here, the filename can be any character(s) and any number of characters.
You can also search files of different extensions by using aterisk (*). The following command will search any files with extension ‘.bash’ or ‘.PNG’
Example – 3: Removing file by partial match and ‘*’
You can use asterisk (*) for matching any filename partially. The following command will remove the file which contains ‘img’ word in any part of the filename.
$ rm *img*
$ ls
Use of question mark (?)
When you know the exact numbers of characters that you want to search then question mark (?) wildcard can be used. The following examples show the different use of question mark (?) wildcard.
Example – 1: Searching file with filename and ‘?’
Suppose, the file extension, the total number of characters of a file and some characters of the file are known, then you can use this wildcard to search the file. The command will search the file which has the extension ‘.PNG’, the word ‘pic’ is at the end of the filename and filename is six character long.
$ ls ???pic.PNG
Example -2: Searching file with extension and ‘?’
Suppose, you know the filename and the total number of characters of the file extension then you can use question mark (?) wildcard to search the file. The following command will search the file with filename ‘mypic’ and the extension is three characters long.
$ ls mypic.???
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.
Example -1: Search files of any extension with two ranges values
The following command will search any file whose name contains any character within ‘p-z’ and any digit within ‘0-9’ and the file extension can be any characters.
$ ls [p-z0-9]*.*
The basic use of wildcard characters is shown in this tutorial. You can also use wildcard characters for creating any regular expression pattern for doing validation work.