Raspberry Pi

Using Wildcard – Raspberry Pi Linux

In the Raspberry Pi system, wildcards are a set of symbols that represent other characters; which are used as a substitute representation for a string or a character. There are three main wildcards which are available for the Raspberry Pi system. In this article, we will discuss the use of wildcards in Raspberry Pi Linux.

Types of Wildcards in Raspberry Pi OS

Raspberry Pi OS is Linux based so all the wildcards available for Linux work on Raspberry Pi too. There are three main wildcards for Raspberry Pi the list of which is mentioned below:

Use of Each Wildcard

Each of the above wildcards is used to serve a different purpose, let’s discuss in detail the use of each wildcard.

Question Mark (?)

The question mark wildcard is used to represent a single character. Using the “?” wildcard, the user can match the single occurrence of any character.

Examples

  • A?z will match anything that starts with the letter A and ends with z and has only one character in between like Aiz, Aoz, Anz, and things like that.

 

  • P??l will match anything that starts with the letter P and ends with l and has two characters in between like Pool, Peel, and Pill.

Asterisk (*)

An asterisk wildcard is used to match any number of occurrences of characters including no character too.

Example

  • k*z will match anything that starts with the letter k and ends with z and has any number of characters in between like kz, kiz, kaaz, kuiezz, and any other occurrences like that.

Bracketed Character [ ]

The Bracketed character wildcard is used only to match the characters that are enclosed in the bracket no matter how much they are in number.

Examples

  • R[eo]d will only match anything that starts with the letter R and ends with d and only has characters “e” or “o” in between like Red, Rod, and Reod.

 

  • R [a-d] m will match anything that starts with the letter R and ends with m and has any characters between a to d like Read, Red, Raed, Rad, Recd, and other such combinations of words.

Using Wildcard in Raspberry Pi Linux Terminal Commands

Now we will share how these wildcards can be used in Raspberry Pi commands, and for that follow the below-mentioned commands.

Command 1: To list all the .txt and .exe files.

$ ls *.txt *.exe

As a result of the above command the list of all .txt and .exe files will appear on the screen.

Command 2: To remove all the .txt files present in a directory.

$ rm *.txt

And to verify if all the text files have been removed or not use the below-mentioned command:

$ ls *.txt

Command 3: To list all the text files that match any of the characters present inside the bracket:

$ ls [abcde]*.txt

Command 4: To list all the files that do not match any character present inside the bracket, basically the “!” sign inside the bracket represents the not state which means whatever is present inside the bracket that shouldn’t be matched. Follow the below command:

$ ls [!abcde]*.txt

Command 5: Another wildcard “#” can also be used, it is not a proper Linux wildcard which is why it wasn’t discussed in the types of wildcard section. This is almost similar to the “*” wildcard, and it is utilized to get the content of the system. The below-mentioned # command can be used to display the list of files present in the system:

$ ls -l #

Command 6: Wildcards can also be utilized to list the particular extensions with files. For instance, below mentioned command can be used to list the files with extensions that start with a “t”:

$ ls *.[t]*

All the wildcards can be used in multiple ways. We have enlisted examples to use wildcards in different ways.

Conclusion

There are three main wildcards that are used in Linux those are asterisk (*), question mark (?), and bracketed character [ ] wildcards. All these wildcards are used to represent characters or strings. In the article, we have shared some commands in which wildcards are used to highlight the usage of wildcards in the Raspberry Pi system.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.