A Wildcard (also known as meta character) is a special character which act as a substitute for a name or extension. We can find these wildcards in both windows and Linux operating systems. In Linux wildcards are mostly used together with shell commands. When we use wildcards in our shell commands it increases the flexibility and efficiency of commands.

Suppose you need to create 50 text files in your home directory. If you are new to Linux and you if you have no idea about wildcards, you will create these 50 files by typing the “touch” command together with the specific filenames 50 times.  But by using wildcards you can create these 50 files only using one command.

So, if you want to perform the same task for a group of files, you don’t have to use the same command repeatedly for each filename in the group. You can use wildcards.

Standard Wildcards (globbing patterns) Used in Linux

According to the man page globbing path names (man glob) there are about 6 Standard Wildcards in Linux. Out of them here are the most frequently used wildcards that you can use to make you day to day tasks easy.

? (question mark)

“?” is used in shell commands to represents exactly one character. For example, if you run the following command,

touch test1 test2 test3 test4 test5
ls –l test?

First Linux would look for all the filenames in the directory which begins with letters “test” respectively. and then it will list all the files which have “test” as their filename no matter what character has after those four letters (“test”).

Also Read: Linux File Permissions Explained

Suppose you create another file called “test123”. This file will not be listed if run the above command. Because “?” wildcard substitutes only a single character.

* (asterisk)

“*” can represent any number of characters. Fr example, if you run the following command,

touch test1 test2 test3 test456 test123 test963 sample.sh saae.sh sarue.sh
ls –l test*

Terminal will show you any file which starts with “test” no matter what characters are present after those four letters (“test”).

If you run,

ls –l sa*e.sh

it will list anything that starts with “sa” and ends with an “e”.

[ ] (square brackets)

These wildcards can represent any characters enclosed in the brackets. For example, if you run the following commands,

ls –l s[a,u,e]m 

It can become sam, sum, sem. If you did,

ls –l s[a-f]m

it can become anything that starts with “a” and ends with “f” and has any character in between “a” to “f”.

Character classes and bracket expressions.

A bracket expression is a list of characters enclosed with [ ] (square brackets). A character class is a predefined set of characters within a bracket expression. There are few Character classes that we can use as wildcards. They are;

  1. alpha – all letters
  2. alnum – letters and numbers
  3. digit – only numbers
  4. lower – lowercase
  5. upper – uppercase
  6. space – files with spaces

Also Read: Python Tutorials | Bubble Sorting Using Python

So if you run the following command it will list all the files which have letters in their file names.

ls –l [[:alpha:]]*

If you run the following command, it will list all the files which have digits in their file names and so on.

ls –l [[:digit:]]*

For more details about Character classes and bracket expressions check out the “grep” man page.

As i mentioned in the beginning of the article here is how to create any number of files using one command.

touch test{0001..0050}

touch is the tool we use to empty files. test is the file name. Next we have a brace expansion. The leading zeros (0050 instead of 50 and 0001 instead of 01) make sure that the output uses zero-padded 4 digits.


Leave a Reply

Your email address will not be published. Required fields are marked *