next up previous contents
Next: Special Characters Up: The Bourne Shell Previous: Redirection and Pipelines

Filename Substitution

The shell allows the user to enter part of a file or several files without typing the filename explicitly. This is called filename substitutiongif and it allows the user to significantly reduce the number of keystrokes entered. For example if someone wanted the listing of all the C source files in the current directory they could type

$ ls *.c

or perhaps all of the C files that start with the letter s:

$ ls s*.c

The * symbol matches a group of characters, but the shell will also recognize two other substitution symbols for less general replacements, the ? and the []. The ? symbol is the character replacement. It will substitute a character into the question mark position of the string until all matches have been made or all the characters have been placed in the spot and no matches have been made. An even more specific substitution exists, the character set substitution. This allows the user to search for a very specific example such as the following:

$ rm file[a-df].txt

This example deletes the files: filea.txt, fileb.txt, filec.txt, filed.txt, filef.txt, but leaves filee.txt untouched (if such a file exists). If the negation symbol ! is used with the character set substitution, the enclosed characters can be explicitly avoided. This means that all strings not containing the substring will be chosen. Of course all of these substitution symbols can be used together as desired.

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998