The square brackets
Square brackets containing one or more characters stand for any
one of the contained characters. For example:
[bch]at
matches bat, cat,
or hat. ls [abc]*
lists all
files in the working directory the names of which start with a, b,
or c, followed by any other sequence of zero or more
characters. In other words, it lists all files whose names start with a, b,
or c.You can specify ranges of characters inside the square brackets
by specifying the first character in the sequence, a hyphen (-),
and the last character. For example:
[a–m]
This
matches any character from a through m.Suppose, for example, that you want to copy the contents of the
working directory into two separate directories. You might enter:
cp [a–m]* dira
to copy all files with
names beginning with the letters a through m to
the directory dira, and then issue the second
command: cp [n–z]* dirb
to copy the rest
of the files to the directory dirb. A command
such as: rm *.[a-z]
removes every file with
a suffix consisting of a single lowercase letter.If the first character inside a bracket construct is an exclamation
mark !, the construct matches any character
that is not inside the brackets. For example:
ls [!a–m]*
lists
any file that does not begin with one of the letters in the range a through m.In the same way:
rm [!0-9]*
removes any
file with a name that does not start with a digit.