Wildcard

A wildcard in Linux is a symbol or a set of symbols that stands in for other characters. It can be used to substitute for any other character or characters in a string. For example, you can use a wildcard to get a list of all files in a directory that begin with the letter O.

Three types of wildcards are common in Linux:

  • ? – matches a single character. For example, O??d matches anything that begins with O, ends with d and has two characters in between (like Oind, Okhd, Oerd, but not Oereed, Oad, Oerererd.)
  • * – matches any character or set of characters, including no character. For example, O*d matches anything that begins with O and ends with d (like Oind, Okhd, Oerd, Oereed, Oad, Oerererd, Od, Oarmeerrd). The number of characters in between O and d is not important.
  • Bracketed values – match characters enclosed in square brackets. For example, O[ac]d matches only Oad and Ocd. You can also specify a range of values: O[a-e]d matches Oad, Obd, Ocd, Odd and Oed.

Let’s look at a couple of examples. We’ve created files mentioned above:

linux files for wildcards

If we want to list all files that begin with O, end with d and have two characters in between, we can use the following syntax:

linux wildcard question mark

To list all files that begin with O and end with d, no matter the number of character in between, we can use the following syntax:

linux wildcard asterix

To list all files that begin with O and end with d and have a or c in between, we need to use the following syntax:

linux wildcard brackets

Subscribe to our Newsletter

Like us on Facebook

top
Geek University 2019