2.5 Wildcards

   

When you want to use many file names in one command, such as the one where grep is used to search a pattern in many files, it is very inconvenient to type all these names at the command line. Wildcard characters are used as a shortcut to refer to many files. Two wildcards are used in UNIX, the asterisk character ( * ) and the question mark ( ? ). The * matches zero or more characters, whereas ? matches only one character. There is a third type of character matching mechanism that checks a range of characters. This is the [] pattern, and a range is specified inside the square brackets. Sometimes this is called the third wildcard.

Use of *

Suppose you use the ls command to list files and the following list appears.

 $  ls  myfile  myfile00  myfile01  myfile010  myf  xyz $ 

Now we can use the * character to list files we want to be displayed. If we want to list all files that start with myfile , the command is:

 $  ls myfile*  myfile  myfile00  myfile01  myfile010 $ 

To list all files that start with my , we use:

 $  ls my*  myfile  myfile00  myfile01  myfile010  myf $ 

Use of ?

The ? matches only a single character. For example, if you want to list all files that start with myfile0 and the last character may be anything, the result is:

 $  ls myfile0?  myfile00  myfile01 $ 

Now try to figure out why myfile010 did not appear in the list.

The wildcard characters can be used wherever you need to specify more than one file. For example, if you want to copy all files from the current directory to the /tmp directory, the command will be:

 $  cp * /tmp  $ 

Similarly, if you want to search for the word root in all files of the /etc directory, you can use this command.

 $  grep root /etc/*  

The wildcard characters are very useful, and if you master these, you can save a lot of time in your daily computer use.

Use of [ ] Wildcard

This wildcard matches a range of characters given inside the square brackets. Only one character from the range is taken. For example [a-m] means any one character between " a " and " m ". Similarly [a,c,x] means character " a ", " c ," or " x ".

 $  ls /etc/[w,x]*  /etc/wall  /etc/whodo  /etc/wtmp  /etc/xtab $ 

The above command lists all files in the /etc directory that start with a " w " or " x " character.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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