Wildcards

Author

Marie-Hélène Burle
Adapted from a Software Carpentry workshop

Wildcards are a convenient way to select items matching patterns.

For this section, we will play with files created by The Carpentries.

You can download them into a zip file called bash.zip with:

wget https://bit.ly/bashfile -O bash.zip

You can then unzip that file with:

unzip bash.zip

You should now have a data-shell directory with a molecules subdirectory.

cd into it:

cd data-shell/molecules

Let’s list the files in the molecules directory:

ls
cubane.pdb  ethane.pdb  methane.pdb  octane.pdb  pentane.pdb  propane.pdb

You could do the same with:

ls *

The star expands to all files/directories matching any pattern. It is a wildcard.

Of course, you can match more interesting patterns.

For instance, to list all files starting with the letter o, we can run:

ls o*
octane.pdb

To list all files containing the letter o anywhere in their name, you can use:

ls *o*
octane.pdb  propane.pdb

This saves a lot of typing and is a powerful way to apply a command to a subset of files/directories.

Your turn:

Wildcards are often used to select all files with a certain extension.

Let’s create 3 new files:

touch file1.txt file2.txt file3.md

How would you list all files with the .txt extension and only those?