Wildcards in Linux Explained with Examples

The wildcards in Linux are built-in building blocks representing other characters commonly used to search for matching patterns. The wildcards are used to simultaneously rename and manipulate several files or directory names, such as replacing a single character or specific characters in names. Linux’s predefined wildcards are designed to make searches more flexible and efficient.

This guide will explain the usage of wildcards in different ways with the help of examples.

What are Wildcards in Linux?

The wildcards are special symbols that are utilized in place of the other characters to match single or multiple characters. The most widely used wildcards with their description are as follows:

CharacterDescription
*This wildcard matches a single or more characters.
?It matches a single character.
[]It matches a range of characters (Multiple characters).

Let’s understand how we can use these wildcards.

Match All Characters Using Wildcard

The most commonly used wildcard is an asterisk (*), which matches every character. For instance, the following command will show the long list of files & directories that start with the “t” character and follows with any character or number:

$ ls -l t*

The output shows all the files that start with the t character and the next characters can be anything.

Match a Single Character Using Wildcard

The question mark (?) is used only to match a single character. To find the files having prefix name as “testfile”, then it matches any character with the question mark (?) wildcard and ends with .txt extension, use the following command:

$ ls -l testfile?.txt

The output shows two files that match the given criteria (testfile?.txt).

Similarly, we can use the single match wildcard question mark (?) in multiple locations. For example, the below command will match any character at two places with the question mark (?) and check for an exact match for other characters:

$ ls -l te?tfile?.txt

The output shows only the files matching the provided criteria (te?tfile?.txt).

Match From Range of Characters Using Wildcard

We can match from a particular range of characters or numbers with the bracket [] wildcard. For example, the below command will find the files & directories having the third character in name from a,s,x,y, or z characters only:

$ ls -l te[asxyz]t.txt

The output shows files matching the given criteria and have third word as s and x from the search range wildcard.

Match All Files Starting With Specific Characters Using Wildcard

We can find the files & directories starting with specific characters (in this case, a to d) in the given directory with the below-stated command:

$ ls -l [a-d]*

It shows all filenames starting with the a,b,c, and d characters only.

Similarly, if you want to find files & directories that do not start with a particular range, we can use the caret (^) symbol. For instance, the below command finds the file & directories that do not start with a range a to d:

$ ls -l [^a-d]*

The output shows that no filename starts with a,b,c, or d characters.

Different Uses of Wildcards in Linux

This section will explain the practical use of wildcards to perform a specific task in Linux.

Example 1: Match File Starting with Uppercase Letter

We can find the files that start with the Uppercase letter with the “[[:upper:]]” syntax. For instance, to list the file that starts with the uppercase letter, run the below-mentioned command:

$ ls -l [[:upper:]]*

The output shows all the files that start with the uppercase letter.

Example 2: Match Filenames that Start with Number

We can use the “[[:digit:]]” syntax to find the filenames that start with the number. For instance, the below command lists those files only that start with the number:

$ ls -l [[:digit:]]*

The output shows that both files start with the number.

Moreover, we can find those files that do not start with the number by using the not symbol (!) in the previous command:

$ ls -l [![:digit:]]*

The output shows files that do not start with the number.

Example 3: Match Files Having Numbers in the Names

We can combine the asterisk symbol (*) and bracket wildcards to find the desired criteria. For example, the below-stated command matches the file names having numbers from 0-9 in the name:

$ ls -l *[0-9]*

The output shows the files with numbers in their names.

Example 4: Match File Having Extensions

We can find the files and directories having extensions. For instance, the below command matches the name “testfile.” and any extension:

$ ls -l testfile.*

The output shows two files that match the criteria (testfile.*)

Example 5: Remove File Having Specific Word

We can remove the files & directories having a specific word in their names. For instance, the below command deletes the files and directories containing the “tar” in their names:

Note: The “v” option is used as a verbose that shows the status of every removed file or directory status.

$ rm -v *tar*

A file having tar in the name is deleted.

Example 6: Check Multiple Wildcards to Find a File or Directory

We can use multiple-range wildcards to check a single character. For example, the below command matches the character after “testfile” that should be from “a-z” or “A-Z”, and “0-9” to match the filename or directory name:

Note: The Linux command-line terminal is case sensitive; so we have to use a-z and A-Z to find both lowercase and uppercase letters.

$ ls testfile[a-zA-Z0-9]*

The output shows the files that match the wildcard criteria.

Conclusion

The most useful wildcards in Linux are an asterisk (*), question mark (?), and bracket [] that represents the other characters to find the specific file or directory names. We can use wildcards to find the specific filename, the file starting or ending with a specific character, number, or extension, as explained with examples throughout this guide.