Filename Expansion: , ?,

 < Day Day Up > 



Filename Expansion: *, ?, [ ]

Filenames are the most common arguments used in a command. Often you may know only part of the filename, or you may want to reference several filenames that have the same extension or begin with the same characters. The shell provides a set of special characters that search out, match, and generate a list of filenames. These are the asterisk, the question mark, and brackets (*, ?, []). Given a partial filename, the shell uses these matching operators to search for files and expand to a list of filenames found. The shell replaces the partial filename argument with the expanded list of matched filenames. This list of filenames can then become the arguments for commands such as ls, which can operate on many files. Table 8-3 lists the shell's file expansion characters.

Table 8-3: Shell Symbols

Common Shell Symbols

Execution

ENTER

Execute a command line.

;

Separate commands on the same command line.

'command'

Execute a command.

$(command)

Execute a command.

[]

Match on a class of possible characters in filenames.

\

Quote the following character. Used to quote special characters.

|

Pipe the standard output of one command as input for another command.

&

Execute a command in the background.

!

History command.

File Expansion Symbols

Execution

*

Match on any set of characters in filenames.

?

Match on any single character in filenames.

[]

Match on a class of characters in filenames.

Redirection Symbols

Execution

>

Redirect the standard output to a file or device, creating the file if it does not exist and overwriting the file if it does exist.

>!

The exclamation point forces the overwriting of a file if it already exists. This overrides the noclobber option.

<

Redirect the standard input from a file or device to a program.

>>

Redirect the standard output to a file or device, appending the output to the end of the file.

Standard Error
Redirection Symbols

Execution

2>

Redirect the standard error to a file or device.

2>>

Redirect and append the standard error to a file or device.

2>&1

Redirect the standard error to the standard output.

>&

Redirect the standard error to a file or device.

|&

Pipe the standard error as input to another command.

Matching Multiple Characters

The asterisk, *, references files beginning or ending with a specific set of characters. You place the asterisk before or after a set of characters that form a pattern to be searched for in filenames. If the asterisk is placed before the pattern, filenames that end in that pattern are searched for. If the asterisk is placed after the pattern, filenames that begin with that pattern are searched for. Any matching filename is copied into a list of filenames generated by this operation. In the next example, all filenames beginning with the pattern "doc" are searched for and a list generated. Then all filenames ending with the pattern "day" are searched for and a list is generated. The last example shows how the * can be used in any combination of characters.

$ ls doc1 doc2 document docs mydoc monday tuesday $ ls doc* doc1 doc2 document docs $ ls *day monday tuesday $ ls m*d* monday $

Filenames often include an extension specified with a period and followed by a string denoting the file type, such as .c for C files, .cpp for C++ files, or even .jpg for JPEG image files. The extension has no special status and is only part of the characters making up the filename. Using the asterisk makes it easy to select files with a given extension. In the next example, the asterisk is used to list only those files with a .c extension. The asterisk placed before the .c constitutes the argument for ls.

$ ls *.c calc.c main.c

You can use * with the rm command to erase several files at once. The asterisk first selects a list of files with a given extension, or beginning or ending with a given set of characters, and then it presents this list of files to the rm command to be erased. In the next example, the rm command erases all files beginning with the pattern "doc":

$ rm doc* 
Tip 

Use the * file expansion character carefully and sparingly with the rm command. The combination can be dangerous. A misplaced * in an rm command without the -i option could easily erase all the files in your current directory.

Matching Single Characters

The question mark, ?, matches only a single incomplete character in filenames. Suppose you want to match the files doc1 and docA, but not document. Whereas the asterisk will match filenames of any length, the question mark limits the match to just one extra character. The next example matches files that begin with the word "doc" followed by a single differing letter:

$ ls doc1 docA document $ ls doc? doc1 docA

Matching a Range of Characters

Whereas the * and ? file expansion characters specify incomplete portions of a filename, the brackets, [], enable you to specify a set of valid characters to search for. Any character placed within the brackets will be matched in the filename. Suppose you want to list files beginning with "doc", but only ending in 1 or A. You are not interested in filenames ending in 2 or B, or any other character. Here is how it's done:

$ ls doc1 doc2 doc3 docA docB docD document $ ls doc[1A] doc1 docA

You can also specify a set of characters as a range, rather than listing them one by one. A dash placed between the upper and lower bounds of a set of characters selects all characters within that range. The range is usually determined by the character set in use. In an ASCII character set, the range "a-g" will select all lowercase alphabetic characters from a through g, inclusive. In the next example, files beginning with the pattern "doc" and ending in characters 1 through 3 are selected. Then, those ending in characters B through E are matched.

$ ls doc[1-3] doc1 doc2 doc3 $ ls doc[B-E] docB docD

You can combine the brackets with other file expansion characters to form flexible matching operators. Suppose you want to list only filenames ending in either a .c or .o extension, but no other extension. You can use a combination of the asterisk and brackets: * [co]. The asterisk matches all filenames, and the brackets match only filenames with extension .c or .o.

$ ls *.[co] main.c  main.o  calc.c

Matching Shell Symbols

At times, a file expansion character is actually part of a filename. In these cases, you need to quote the character by preceding it with a backslash to reference the file. In the next example, the user needs to reference a file that ends with the ? character, answers?. The ? is, however, a file expansion character and would match any filename beginning with "answers" that has one or more characters. In this case, the user quotes the ? with a preceding backslash to reference the filename.

$ ls answers\? answers?

Generating Patterns

Though not a file expansion operation, {} is often useful for generating names that you can use to create or modify files and directories. The braces operation only generates a list of names. It does not match on existing filenames. Patterns are placed within the braces and separated with commas. Any pattern placed within the braces will be used to generate a version of the pattern, using either the preceding or following pattern, or both. Suppose you want to generate a list of names beginning with "doc", but only ending in the patterns "ument", "final", and "draft". Here is how it's done:

$ echo doc{ument,final,draft} document docfinal docdraft

Since the names generated do not have to exist, you could use the {} operation in a command to create directories, as shown here:

$ mkdir {fall,winter,spring}report $ ls fallreport winterreport springreport



 < Day Day Up > 



Red Hat(c) The Complete Reference
Red Hat Enterprise Linux & Fedora Edition (DVD): The Complete Reference
ISBN: 0072230754
EAN: 2147483647
Year: 2004
Pages: 328

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