Chapter 33. Wildcards

File-Naming Wildcards

Wildcards (Section 1.13) are the shell’s way of abbreviating filenames. Just as in poker, where a wildcard is a special card that can match any card in the deck, filename wildcards are capable of matching letters or groups of letters in the alphabet. Rather than typing a long filename or a long chain of filenames, a wildcard lets you provide parts of names and then use some “wildcard characters” for the rest. For example, if you want to delete all files whose names end in .o, you can give the following command:

% rm *.o

You don’t have to list every filename.

I’m sure you already know that wildcards are useful in many situations. If not, they are summarized in Section 33.2. Here are a few of my favorite wildcard applications:

  • If you remember part of a filename, but not the whole name, you can use wildcards to help you find it. If I have a file on genetics saved in a directory with several hundred other files, a command like:

    % ls *gene*

    will often find what I want. It’s quicker and easier than find (Section 9.1).

  • Wildcards are a natural when you want to work with groups of files. If I have a general purpose directory that’s full of filenames ending in .c and .h, I can make new subdirectories and use wildcards to move the files easily:

    % mkdir c h
    % mv *.c c
  • Wildcards often help you to work with files with inconvenient characters in their names. Let’s say you have a file named abc x e, where x is some unknown control character. You can delete or ...

Get Unix Power Tools, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.