Standard wildcards and globbing patterns can be used in almost all command-line utilities in Linux. Here we will discuss some of the most widely used wildcards and globbing patterns and give some examples of how to use them.

Star / Asterisk - *

The star wildcard or asterisk can represent any number of characters, to be more precise it can represent zero or more characters.

“st*” will match anything starting with “st”, including “st” alone.  “*.txt” will match any file ending in the “.txt” file extension, and “*test*” will match any file with test in it’s name.

List all files starting with “test”:

$ ls test*
testnopreserve.txt  testpreserve.txt

All files with “test” in their name:

$ ls *test*
savonatest.txt  testnopreserve.txt  testpreserve.txt

List all files ending in “.txt” file extension:

$ ls *.txt
hammer.txt  savonatest.txt  testnopreserve.txt  testpreserve.txt

Question Mark - ?

The question mark can represent any single character. This is not limited to alphanumeric characters.

List all files that have a single character before “ammer.txt”.

$ ls ?ammer.txt
hammer.txt  Yammer.txt

Here is an example using a non-alphanumeric character.

$ ls ?k.txt
%k.txt  ok.txt

Square Brackets = []

Square brackets allow you to specify a range and characters or numbers. You can use [a-z] which would mean all lower case letters as well as [0-9] which would mean all digits.

List all files that have ANY (because we are using the range “a” though “z”) lowercase letter, followed by “k.txt”:

$ ls [a-z]k.txt
ok.txt  zk.txt

Now we narrow the range to “a” though “p” and only receive one result:

$ ls [a-p]k.txt
ok.txt

The same can be done using a range of numbers.

List all files that are file2.txt through file4.txt:

$ ls file[1-4].txt
file1.txt  file2.txt  file3.txt  file4.txt

Curly Brackets - {}

Curly brackets are used for multiple matches.  Each string can be an exact name, or a wildcard. It will find anything that matches any of the given strings using an or relationship (one OR the other).

For example, if I had a directory with a lot of txt and doc files, I could copy them like so:

$ cp -v {*.doc,*.txt} /tmp
‘check.doc’ -> ‘/tmp/check.doc’
‘savona.doc’ -> ‘/tmp/savona.doc’
‘wildcard.doc’ -> ‘/tmp/wildcard.doc’
‘file1.txt’ -> ‘/tmp/file1.txt’
‘file2.txt’ -> ‘/tmp/file2.txt’
‘file3.txt’ -> ‘/tmp/file3.txt’
‘file4.txt’ -> ‘/tmp/file4.txt’
‘file5.txt’ -> ‘/tmp/file5.txt’
‘hammer.txt’ -> ‘/tmp/hammer.txt’
‘%k.txt’ -> ‘/tmp/%k.txt’
‘ok.txt’ -> ‘/tmp/ok.txt’
‘savonatest.txt’ -> ‘/tmp/savonatest.txt’
‘testnopreserve.txt’ -> ‘/tmp/testnopreserve.txt’
‘testpreserve.txt’ -> ‘/tmp/testpreserve.txt’
‘Yammer.txt’ -> ‘/tmp/Yammer.txt’
‘zk.txt’ -> ‘/tmp/zk.txt

NOTE: You can use any of the other wildcards inside of the curly brackets.

Logical NOT - [!]

This wildcard is used to NOT match something, or to exclude.  Anything inside the square brackets that is following an exclamation point will be excluded in any matches.

Remove all files named file*.txt except file2.txt:

$ rm -v file[!2].txt
removed ‘file1.txt’
removed ‘file3.txt’
removed ‘file4.txt’
removed ‘file5.txt’

Backslash - \

A backslash is used to escape (or to make literal) a special character.  For example, what if you wanted to match a literal asterisks? The shell would see that as a wildcard as explained above.

EXAMPLE: Here we are searching for a file name with an asterisks in it (This is not recommended, just using it as an example).  But it returns other filenames because it interprets the asterisk as a wildcard.

$ ls notawild*.txt
notawildcard.txt  notawildone.txt  notawild*.txt

If we escape it using the backslash, the shell interprets the asterisks and a literal asterisks and only returns the one file.

$ ls notawild\*.txt
'notawild*.txt'

SIDE NOTE: You can also use single quotes to tell the shell not to expand wildcards, like so:

$ ls 'notawild*.txt'
notawild*.txt

Conclusion

That is wildcards and globbing in a nutshell. When you get used to using them they will save you a ton of time. You will also notice that your commands can get very complicated very fast. If you have any exampled of really complex examples feel free to post about them in the comments.