The use of
glob characters in Linux is similar to what many operating systems refer to as "wildcard" characters. Using glob characters, you match filenames using patterns.
Glob characters are a shell feature, not something that is particular to any specific command. As a result, you can use glob characters with any Linux command.
When glob characters are used, the shell will "expand" the entire pattern to match all files in the specified directory that matches the pattern.
For demonstration purposes, we will use the echo
command to display this expansion process.
Use the following echo
command to display all filenames in the current directory that matches the glob pattern "*":
echo *
Your output should be similar to the following:
The asterisk "*" matches "zero or more" characters in a file name. In the example above, this results in matching all filenames in the current directory.
The echo
command, in turn, displays the filenames that were matched.
The following commands will display all the files in the current directory that start with the letter D, and the letter P:
echo D*
echo P*
Your output should be similar to the following:
Think of the first example, D*, as "match all filenames in the current directory that begin with a capital d character and have zero or more of any other character after the D".
The asterisk (*) can be used anywhere in the string. The following command will display all the files in your current directory that end in the letter s:
echo *s
Your output should be similar to the following:
Notice that the asterisk can also appear multiple times or in the middle of several characters:
echo D*n*s
Your output should be similar to the following:
The next glob metacharacter that we will examine is the question mark (?). The question mark matches exactly one character. This single character can be any possible character.
Like the asterisk it can be used anywhere in a string and can appear multiple times.
Since each question mark matches one unknown character, typing six of them will match six character filenames. Type the following to display the filenames that are exactly six characters long:
echo ??????
Your output should be similar to the following:
Warning: Each ?
character must match exactly one character in a filename, no more and no less
than one character.
Using the question mark with other characters will limit the matches. Type the following to display the file names that start with the letter D and are exactly nine characters long:
echo D????????
Your output should be similar to the following:
Wildcards or glob characters can be combined together. The following command will display file names that are at least six characters long and end in the letter s.
echo ?????*s
Your output should be similar to the following:
Think of the pattern ?????*s to mean "match filenames that begin with any five characters, then have zero or more of any characters and then end with an s character".
The next glob is similar to the question mark glob to specify one character.
This glob uses a pair of square brackets ( [ ] ) to specify which one character will be allowed. The allowed characters can be specified as a range, a list, or by what is known as a character class.
The allowed characters can also be negated with an exclamation point "!".
In the first example, the first character of the file name can be either a D or a P. In the second example, the first character can be any character except a D or P:
echo [DP]*
echo [!DP]*
Your output should be similar to the following:
In these next examples, a range of characters will be specified. In the first example, the first character of the file name can be any character starting at D and ending at P. In the second example, this range of characters is negated, meaning any single character will match as long as it is not between the letters D and P:
echo [D-P]*
echo [!D-P]*
Your output should be similar to the following:
You may be asking yourself "who decides what letters come between D and P?". In this case the answer is fairly obvious (E, F, G, H, I, J, K, L, M, N and O), but what if the range was [ 1-A]?
The ASCII text table is used to determine the range of characters. You can view this table by searching for it on the Internet or typing the following command: ascii
So, what characters does the glob [1-A] match? According to the ACSII text table: 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @ and A.
Keyword:
Netacad.com
Module Cisco
Exam Netacad