Wildcards – Part One

As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, we build upon some of the topics covered in our Linux Fundamentals course, including files and shell scripting.

Learning Objectives

  • Learn what wildcards are and how and when to use them
  • Understand input, output, and redirection
  • Work with files and shell scripting

Intended Audience

  • Anyone with basic knowledge of Linux who wants to learn more
  • Professionals who want to learn more about Linux to enhance their career prospects


This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.


In this lesson, you'll learn what wildcards are, when you can use them and the various types of wildcards. Finally, you'll get to look over my shoulder as I run through several examples of how to use wildcards on an actual Linux system.

A wildcard is a character or a string that is used to match file and directory names. You can use wildcards to create search patterns, that when expanded will return a list of matching files and directories. Sometimes wildcards are referred to as globs or glob patterns.

Globbing is the act of expanding a wildcard into the list of matching files and directories. Wildcards can be used in conjunction with most Linux commands. If a command accepts a file or directory as an argument, then you can use a wildcard in the argument to specify a file or set of files. You'll find yourself using wildcards with commands like, LS, RM, CP, MV and others.

Here are the two main wildcards. They are the asterisk and the question mark. The asterisk or star as I like to call it, matches zero or more characters. It matches anything. By itself it's not really that useful, but when you combine it with other parts of file or directory names that you're looking for, it becomes powerful. For example, we could use *.txt to find all the files that end in .txt. If you wanted to list all the files that start with a letter A, then use a*. If you want to find all the files that start with an A and end in .txt, then use, a*.txt. The question mark matches exactly one character.

If you want to find all the files that only have one character proceeding .txt, then use ?.txt. To match all the two letter files that begin with an A use the letter, A followed by a question mark. To match all the files that start with A and then are followed by exactly one more character, and then end in .txt, use a?.txt.

You can use something called a character class to create very specific search patterns. Start with a left bracket, then list one or more characters you want to match and then end with a right bracket. For example, if you wanted to match a one character long file name that was a vowel, you would use [aeiou].

If you wanted to match files that start with CA followed by either an N or a T followed by zero or more characters then use, ca[nt]*. This pattern will match files named, can, cat, candy and catch. If you want to exclude characters in a match, use an exclamation mark.

For example, if you wanted to find all the files that do not start with a vowel, use [!aeiou]*. Baseball and cricket match this pattern because the first character is not an A E I O or a U. When using character classes, you can create a range by separating two characters with a hyphen. If you want to match all the characters from A to G use, [a-g]*. If you want to match the numbers, three, four, five and six, use [3-6]*. Instead of creating your own ranges, you can use predefined named character classes. These named character classes represent the most commonly used ranges.

Alpha matches alphabetic letters. This means it matches both lower and uppercase letters. A, L, N, U, M or alnum matches alphanumeric characters. This means it matches alpha and digits. Set another way, it matches any uppercase or lowercase letters or any decimal digits. Digit represents the numbers in decimal from zero to nine. Lower matches any lower case letters. Space matches white space. This means characters such as spaces, tabs and new line characters. Upper only matches upper case letters.

What if you want to match one of the wildcard characters? Then you would escape that character with a backslash. To escape the wildcard, simply place the backslash before the wildcard character. If you want to make your life easier, don't name your files with question marks and asterisks. However, you may end up receiving a file with these characters in them, so you'll need to know how to handle them. For example, if you wanted to match all the files that end with a question mark, then you *\? An example match would be a file named, done?

About the Author
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.