Wildcard

Introduction

Wildcards are a powerful feature for Unix. We can use wildcards almost all commands. In particular, three wildcards are most common: *, ?, and [].

Asterisk (*)

The most important wildcard is the asterisk (*) that indicates the all. When you want to list the text files in your directory, you can do it with the wildcard asterisk:

$ ls *.txt

If you want to show a list of files starting from a character “a”, you can do it like this:

$ ls a*

or

$ ls a*.*

You can also use the wildcard asterisk in the middle of the filename.

$ ls *test*

A question mark (?)

The second important wildcard is the question mark (?) that indicates one character of something. When you want to find the file “Yoshi.txt” but you forgot whether it was “Yoshy.txt”, “Yoshe.txt”, or “Yoshi.txt”, you can use the wildcard to show it like this:

$ ls Yosh?.txt

This wildcard is also useful if you want to list the monthly reanalysis data, for example, in the 1980s:

$ ls uwnd.198?.nc

The wildcard “?” is applicable to one character only whereas the wildcard “*” allows multiple characters.

Range []

The third useful wildcard is parenthesis [] that indicates a range. When you want to list a range of numbers, for example from 4 to 7, you can use the wildcard [4-7]. So, when you have a list of files, such as text1.txt, text2.txt, text3.txt, …, and text10.txt, you can pick up text4.txt, text5.txt, text6.txt, and text7.txt by doing this:

$ ls text[4-7].txt

This wildcard is also applicable to the alphabet.