Filename Generation

Filename generation is when you specify a pattern to a UNIX shell, and it replaces pattern with the names of all files that match pattern. Some simple examples of pattern might be:

rm *
ls -l get_buf.?

Another commonly used term for this is called wildcard expansion.

zsh offers very powerful and expressive filename generation - it goes far beyond what all other UNIX shells can do. To disable filename generation, turn off the GLOB option. (unsetopt GLOB)

What constitutes a pattern?

If zsh sees one of the following characters (unquoted) in a word, zsh treats that word as a pattern for filename generation:

* ( | < [ ?

If the EXTENDED_GLOB option is set, zsh will also recognize the following:

^ #

If one or more files match the pattern, zsh replaces that word with a sorted list of the matching filenames. If no match is found, you can configure zsh's behavior:

Zsh will: If you:
Display an error message do nothing (default behavior)
Delete the word from the command line set the NULL_GLOB option
Leave the word unchanged set the NOMATCH option

Recursive Globbing

Zsh offers an extremely useful way to match files recursively. Use **/, and it will search all directories recursively. Use ***/ if you want zsh to follow symlinks.

For example, to find all .c, .h, and .o files in a directory and all of its subdirectories, enter something like:

  lyric > print -l /usr/local/src/**/*.[cho]

More about wildcard patterns on the next page.

NOTE: Matching / and .

There are a a few special rules you should keep in mind when it comes to matching files containing dots (.) and slashes (/).

1. A / must be matched explicitly.

If you want to match a direcotry, you have to put any /'s in pattern explicitly.

2. A . must be matched explicitly at the start of a pattern or immediately after a /.

Files starting with a dot won't be returned from wildcards unless you put an actual dot at the beginning of pattern. (Or after a /).

3. No filename generation pattern matches the files '.' or '..'

. and .. will never show up in any wildcard match.

Note that rule 2 does not apply if the option GLOB_DOTS is set.

Related Items