Pattern matching using [ ] shell metacharacters
Metacharacters offer another type of wildcard notation by enclosing
the desired characters within [ ]
. It is like using the ?
,
but it allows you to choose specific characters to be matched.
The
[ ]
also allow you to specify a range of values
using the hyphen (-
). To specify all the letters in the alphabet,
use [[:alpha:]]
. To specify all the lowercase letters in
the alphabet, use [[:lower:]]
.See the following examples:
- To refer to only the files that end in
1
or2
, use:
The files selected would be: afile1, afile2, file1, and file2.*file[12]
- To refer to only the files that start with any number, use:
The files selected would be: 1test and 2test.[0123456789]* or [0-9]*
- To refer to only the files that do not begin with an a, use:
The files selected would be: 1test, 2test, bfile1, file, file1, file10, file2, and file3.[!a]*