Globbing is a way of expanding wildcard characters in a
non-specific file name into a set of specific file names. Shells use globbing
all the time. For instance:

$ ls foo*
foo1 foo2

When you enter the above, you’re using a glob: foo*. What the shell is doing, however,
is interpreting that wildcard character (*) and then passing the actual files
to list to the ls command; so in this
instance, the command that is actually being used is ls foo1 foo2.

Some standard wildcards that are often used include:

  • the asterisk (*) character for zero or more
  • the question mark (?) character for any single
  • the [123] specifier, which indicates any of the
    characters 1, 2, or 3
  • the [0-9] specifier, which indicates any number
    between 0 and 9 (or [a-z] for any letters between a and z).

You can also use named ranges, such as:

  • [::alpha::] for any letter
  • [::alnum::] for any letter or number
  • [::lower::] for any lowercase character

When it comes to globbing, zsh is one of the most powerful
shells with some very interesting wildcard characters that can be used. For
instance, you can use glob qualifiers, which are characters with a special
meaning. If you wanted to list all the symbolic link files in the current
directory you would use:

$ ls *(@)

Some other zsh glob qualifiers include a forward slash (/) for
directories and a period (.) for plain files; the zshexpn man page has a full list. If there are no matches, zsh will
return an error; if there is nothing to pass to the ls program (or any other program), the program will not even be
called by zsh.

There are other useful patterns, such as the (**/) pattern. What
this does is tell zsh to match any number of directories to any depth,
including the current directory. To find all the (*.sh) and (*.py) files in the
current directory and sub-directories in a list suitable for parsing (i.e.,
perhaps in a script), you would use:

$ ls -1 **/*.(sh|py)

Finally, to enable extended globbing at all times, add this to

setopt extended_glob

If you do decide to use extended globbing, be aware of special
characters in file names and the need to quote them. For instance, if you want
to copy files from a remote site using scp, you don’t want zsh to expand any wildcards
locally; you want that wildcard passed to scp for files on the remote end to
match, so instead of:

$ scp hades:~/tmp/* .
zsh: no matches found: hades:~/tmp/*

You would need to use:

$ scp "hades:~/tmp/*" 

Delivered each Tuesday, TechRepublic’s free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!