next up previous contents
Next: Filename Completion Up: The C Shell Previous: Redirection and Pipelines

Filename Substitution

The C shell allows the user to specify a filename or group of filenames without having to explicitly type in the entire filename. This is called filename substitution. For instance, the user might wish to list all of the C source files in a directory, or perhaps list all but the C source files in a directory. The most common type of of filename substitution is the metacharacter (*). This character tells the shell to substitute in an arbitrary list of characters until all possible matches have been made. For example,

% ls *.c
 prog.c      prog2.c        string_search.c
% rm p*.c
% ls *.c

This type of substitution is quite robust since any number of strings will be tried until the appropriate ones have been chosen. There are however, more specific substitutions that can be made. The (?) character tells the shell to only try and substitute the one character until matches are made:

% rm pro?2.c

while this looks like a job for the metacharacter (*), it must be remembered that if a file called program2.c existed, it would be destroyed if the (*) was used rather than the (?). The [] substitution will try to insert each character in the brackets into the filename. If a range is specified by use of a dash, each character in the range will be tried, as well as any other characters in the brackets. For example, rm file[a-df].txt would delete all of the following files: filea.txt, fileb.txt, filec.txt, filed.txt, and filef.txt if they existed. The negation character (^) could be used to prevent substitution of characters in similar fashion. All of the above were provided in the Bourne shell, but the C shell provides a couple more types of filename substitution. The curly brace {} will tell the shell to insert each provided string, separated by commas, into the filename:

% cp data{june,july,august}.95 old_data

would copy the files datajune.95, datajuly.95, and dataaugust.95 into the old_data directory.

While it may seem that every conceivable type of filename substitution has been covered, the C shell recognizes a special character that can be best described as yet another. In the C shell, the (~) character is recognized as the current user's home directory. If the user types cd ~ anywhere on the system he will be returned to his home directory. If the tilde (~) is followed by the user name of another user on the system, this user's home directory is the result. For example,

% cp file ~smith

will copy file to user Smith's home directory if the appropriate write permissions are set.

next up previous contents
Next: Filename Completion Up: The C Shell Previous: Redirection and Pipelines

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