Updated April 14, 2023
Introduction to Linux wildcards
A wildcard in Linux means it might be a symbol or set of symbols representing other characters. It is generally used in substituting any string or a character. In this article, we will learn what are the wildcard options that can be used in Linux with a brief description and screenshots.
Syntax of Wildcards used in Linux
The basic set of wildcards in Linux includes below:
- * – This wildcard represents all the characters
- ? – This wildcard represents a single character
- [] – This wildcard represents a range of characters.
Types of Linux Wildcards
There are mainly three types of Wildcards in Linux. Below are the wildcards explained briefly for better understanding. They are:
- Question Mark Wildcard ‘?’ – The wildcard ‘?’ means it will match a single character. For example, S?? n will match anything that will begin with S and end with n and has two characters between them.
- Star Wildcard ‘*’ – The wildcard ‘*’ means it will match any number of characters or a set of characters. For example, S**n will match anything between S and n. The number of characters between them do not count.
- Bracket value Wildcard ‘[]’: The wildcard ‘[]’ means it will match characters that are enclosed in square braces. For example, S[on]n will match only Son and Snn. We can also specify characters in braces like S[a-d]n, which will match San, Sbn, Scn, Sdn.
How do wildcards work in Linux?
Wildcards are mainly used to increase the efficiency and flexibility of searches in Linux. They are generally used in shell commands to execute the commands that are used to display the result. They are also mainly used in regular expressions to match the patterns in the system.
Below are the options that can be used in Linux Wildcards to find pattern matching strings in Linux.
Options | Description |
* | To list out all the files. |
A* | Any file that begins with the letter ‘A.’ |
A*txt | Any file that begins with the letter ‘A’ and ends with a txt |
List?? | Any file that begins with List followed by 2 characters. |
[abc]* | Any file that begins with either ‘a’ or ‘b’ or ‘c’ and ending with any number of characters. |
[[:upper:]] | Any file that begins with an uppercase letter |
[![:digit:]]* | Any file that does not begin with a numeral |
[a-d]? | Any file that begins from a range of a-d and followed by exactly 1 character. |
A??f | Any file that begins with the letter ‘A’ followed by exactly two characters and ending with ‘f’. |
Examples of Wildcards in Linux
Let us take the below example where there are few files. We will take this example and do pattern matching with wildcards in Linux.
1. Option with ‘?’
When we use the wildcard as ‘?’, it will search for characters starting with S and ending with f and exactly one character in between them.
Syntax:
ll A?f
Example: Here, we can see that files starting with ‘A’ followed by exactly one character and ending with ‘f’ are displayed.
2. Option with ‘*.’
The wildcard ‘*’ means it will match any number of characters or a set of characters. For example, S**n will match anything between S and n. The number of characters between them do not count.
Syntax:
ll A*f
Example: Here, we can see in the result that files starting with ‘A’ and ending with ‘f’ are displayed. Here ‘*’ can include any number of characters in between them.
3. Option with []
The wildcard ‘[]’ means it will match characters that are enclosed in square braces.
Syntax:
ll A[b-c]f
Example: Here, we can see in the result that files starting with ‘A’ followed by a range of characters from b to c and ending with f are displayed.
4. Combination of all wildcards
When we try to search for any pattern in Linux, there might be a situation where we would need to combine one or more wildcards with matching the pattern that we are searching for.
Syntax:
ls -l ?*f
Example: Here, one character and any number of characters are displayed with ending characters as f are displayed in the result set.
5. Option with wildcards
When we try to search any pattern in Linux, there might be a situation where we would need to combine one or more wildcards with matching the pattern that we are searching for.
Syntax:
ls -l [Bl]*
Example: Here in the result set, we can see that files with either ‘B’ or ‘l’ are displayed, followed by any number of characters.
6. Options with numerals
We can also use numerical ranges in bracketed value wildcards to find pattern matching strings.
Syntax:
ls -l ?[0-9]*
Example: We can also use numerical ranges in bracketed value wildcards to find pattern matching strings. Here the beginning of the result should be exactly one character followed by a range of numerals from 0 to 9.
7. Option for wildcard ‘#’
The wildcard ‘#’ is also similar to wildcard ‘*’, where it will display all the contents in the system.
Syntax:
ls -l #
Example: To display all the files in the system, we can use the wildcard option ‘#’.
Conclusion
A wildcard in Linux means it might be a symbol or set of symbols representing other characters. It is generally used in substituting any string or a character. Wildcards are mainly used to increase the efficiency and flexibility of searches in Linux. They are generally used in shell commands to execute the commands that are used to display the result. They are also mainly used in regular expressions to match the patterns in the system.
We hope that the above article will give you complete knowledge of wildcard commands in Linux.
Recommended Articles
We hope that this EDUCBA information on “Linux wildcards” was beneficial to you. You can view EDUCBA’s recommended articles for more information.