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.
“*” 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;
- alpha – all letters
- alnum – letters and numbers
- digit – only numbers
- lower – lowercase
- upper – uppercase
- space – files with spaces
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 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.