Filename Substitution

Filename Substitution

It is frequently convenient to use so-called wildcard characters when typing in a command line. As the name implies, a wildcard character is a character that stands for zero or more other characters.

*

Matches zero or more characters.

?

Matches any single character.

[]

Defines a set of characters.

{}

Defines a set of character sequences.

^

Can be used to negate character set definitions.

The most powerful of the wildcards is the Kleene star, '*', which is the shift of the 8 on your keyboard. It will match any character string of any length. For example, to display all files beginning with the letter t, use the following command:

ls t*

To remove all files ending with the .txt extension and beginning with the letter c, use the following:

rm c*.txt

The question mark character matches any single character. For example, the command

ls alan.?

might return all of the following:

alan.c alan.o

Note that the ? character does not match 0 characters. That is, a file such as

alan

would not have been returned by the aforementioned command.

The square braces [] can be used to define a set of characters. For example, the command

ls [br]ob

would list files named bob and rob (but not one named brob). Character sets can be negated. To list files ending in 'ob', but beginning with any single character that is not 'b' or 'r', use the command

ls [^br]ob

which might return

job hob lob

Where the square braces define sets composed of individual characters, the squiggly braces '{}' define sets composed of character sequences. For example, the command

ls {kn, gl}ob

would match against files named 'knob' and 'glob'. Unlike sets of single characters, sets of multiple characters cannot be meaningfully negated.

The final wildcard character is the tilde '~', which refers to the home directory of a user. With no additional characters, tilde is taken to refer to the home directory of the current user. If you wish to refer to a user other than yourself, append that username to the tilde. For example,

~jason

would refer to the home directory of the user jason, assuming such a user exists.

If for some reason you wish to disable filename substitution, you can set the noglob shell variable.

set noglob

 



Linux Desk Reference
Linux Desk Reference (2nd Edition)
ISBN: 0130619892
EAN: 2147483647
Year: 2000
Pages: 174
Authors: Scott Hawkins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net