Bash wildcards: ? (question mark) and * (asterisk)

The usage of wildcards is a nice feature that the shell provides us. Simple characters such as * or ? can make our bash life much more easier than we have ever imagined.

Watch Free Movies

Wildcards are special characters that allow us to select a big number of files that match a specified pathern. The most common are the asterisk * and the question mark ? , which I will teach you in the article.

Every shell has it’s own wildcards! Not all the bash wildcards work in zsh, for example!

The bash asterisk * wildcard:

The asterisk represents zero or more characters:
The following command will move the .txt files to ~/dir :

$ ls
a.txt  bb.txt  ccc.txt  d  e  f
$ mv *.txt ~/dir
$ ls ~/dir
a.txt bb.txt ccc.txt

More examples of using the asterisk * wildcard here.

The bash question mark ? wildcard:

The question mark represents only one character.

ls ??.txt lists all the files with the .txt extension that have only two characters in their name and ls ? lists all the files with names containing only one character:

$ ls
a.txt bb.txt ccc.txt d e f xx.txt
$ ls ??.txt
bb.txt xx.txt
$ ls ?
d e f

If you want to learn about other wildcards, read this article.

Scroll to Top