Sunday, October 25, 2009

Filename Substitution








 

 










Filename Substitution



The Asterisk



One powerful feature of the Unix system that is actually handled by the shell is filename substitution. Let's say that your current directory has these files in it:





$ ls

chapt1

chapt2

chapt3

chapt4

$



Suppose that you want to print their contents at the terminal. Well, you could take advantage of the fact that the cat command allows you to specify more than one filename at a time. When this is done, the contents of the files are displayed one after the other:





$ cat chapt1 chapt2 chapt3 chapt4

...

$



But you can also type in





$ cat *

...

$



and get the same results. The shell automatically substitutes the names of all the files in the current directory for the *. The same substitution occurs if you use * with the echo command:





$ echo *

chapt1 chapt2 chapt3 chapt4

$



Here the * is again replaced with the names of all the files contained in the current directory, and the echo command simply displays them at the terminal.



Any place that * appears on the command line, the shell performs its substitution:





$ echo * : *

chapt1 chapt2 chapt3 chapt4 : chapt1 chapt2 chapt3 chapt4

$



The * can also be used in combination with other characters to limit the filenames that are substituted. For example, let's say that in your current directory you have not only chapt1 through chapt4 but also files a, b, and c:





$ ls

a

b

c

chapt1

chapt2

chapt3

chapt4

$



To display the contents of just the files beginning with chapt, you can type in





$ cat chapt*

.

.

.

$



The chapt* matches any filename that begins with chapt. All such filenames matched are substituted on the command line.



The * is not limited to the end of a filename; it can be used at the beginning or in the middle as well:





$ echo *t1

chapt1

$ echo *t*

chapt1 chapt2 chapt3 chapt4

$ echo *x

*x

$



In the first echo, the *t1 specifies all filenames that end in the characters t1. In the second echo, the first * matches everything up to a t and the second everything after; thus, all filenames containing a t are printed. Because there are no files ending with x, no substitution occurs in the last case. Therefore, the echo command simply displays *x.



Matching Single Characters



The asterisk (*) matches zero or more characters, meaning that x* matches the file x as well as x1, x2, xabc, and so on. The question mark (?) matches exactly one character. So cat ? prints all files with one-character names, just as cat x? prints all files with two-character names beginning with x.





$ ls

a

aa

aax

alice

b

bb

c

cc

report1

report2

report3

$ echo ?

a b c

$ echo a?

aa

$ echo ??

aa bb cc

$ echo ??*

aa aax alice bb cc report1 report2 report3

$



In the preceding example, the ?? matches two characters, and the * matches zero or more up to the end. The net effect is to match all filenames of two or more characters.



Another way to match a single character is to give a list of the characters to use in the match inside square brackets [ ]. For example, [abc] matches one letter a, b, or c. It's similar to the ?, but it allows you to choose the characters that will be matched. The specification [0-9] matches the characters 0 through 9. The only restriction in specifying a range of characters is that the first character must be alphabetically less than the last character, so that [z-f] is not a valid range specification.



By mixing and matching ranges and characters in the list, you can perform some complicated substitutions. For example, [a-np-z]* matches all files that start with the letters a through n or p through z (or more simply stated, any lowercase letter but o).



If the first character following the [ is a !, the sense of the match is inverted. That is, any character is matched except those enclosed in the brackets. So





[!a-z]



matches any character except a lowercase letter, and





*[!o]



matches any file that doesn't end with the lowercase letter o.



Table 2.1 gives a few more examples of filename substitution.





















































































Table 2.1. Filename Substitution Examples


Command





Description





echo a*





Print the names of the files beginning with a





cat *.c





Print all files ending in .c





rm *.*





Remove all files containing a period





ls x*





List the names of all files beginning with x





rm *





Remove all files in the current directory (Note: Be careful when you use this.)





echo a*b





Print the names of all files beginning with a and ending with b





cp ../programs/* .





Copy all files from ../programs into the current directory





ls [a-z]*[!0-9]





List files that begin with a lowercase letter and don't end with a digit














     

     


    No comments:

    Post a Comment