University Systems --> TerpConnect --> General TerpConnect Information --> Examples of Using Wild Cards in Unix -->

Examples of Using Wild Cards in Unix

For the following examples we will use the echo command, because echo does nothing more than print out whatever arguments it was given on the command line.

Suppose your directory contained the files:

> ls
Howdy          file.3          file_ii          howdy         test.3
file          file.4          file_iii          test         test.4
file.          file.5          file_iv          test.         test.5
file.1          file.6          file_v          test.1         test.6
file.2          file_i          file_vi          test.1
  • Since * matches any sequence of characters, it would produce the full listing of every file in the directory:
        > echo *
        Howdy file file. file.1 file.2 file.3 file.4 file.5 file.6 file_i file_ii file_iii file_iv file_v file_vi howdy test test. test.1 test.2 test.3 test.4 test.5 test.6
    
  • "H*" would match only the file that began with H, and would match any characters after the H, specifically Howdy.
        > echo H*
        Howdy
    
  • With "file*", file would get matched since * can also match an empty string (i.e. it need not match anything within a word).
    > echo file*
    file file. file.1 file.2 file.3 file.4 file.5 file.6 file_i file_ii file_iii file_iv file_v file_vi
    
  • Using '*' within the constraints that each file must begin with the string file, and end with the character i:
    > echo file*i
    file_i file_ii file_iii file_vi
    
  • "file?" would match only the file file., since that is the only file in the directory named file that has a single character following it. The ? wild card must match a single character.
    > echo file?
    file.
    
  • "file.[0-9]" would only match those files who's name begins with file. and ends with a single digit (between 0 and 9).
    > echo file.[0-9]
    file.1 file.2 file.3 file.4 file.5 file.6
    
  • "test[.0-9]" matches only test. since that is the only file beginning with test and ends with one of the characters of '.' and a digit.
    > echo test[.0-9]
    test.
    
  • Since { } works on a comma separated list of strings, the shell searches for files beginning with file and test followed by the string .1 which produces the following:
    > echo {file,test}.1
    file.1 test.1
    
  • As stated, you can combine wild cards together so "{file,test}.[0-9]" would produce:
    > echo {file,test}.[0-9]
    file.1 file.2 file.3 file.4 file.5 file.6 test.1 test.2 test.3 test.4 test.5 test.6
    
  • As a little more complicated example of combining wild cards, "{file,test}.{[0-9],i}" would produce:
    > echo {file,test}.{[0-9],i}
    file.1 file.2 file.3 file.4 file.5 file.6 file.i test.1 test.2 test.3 test.4 test.5 test.6 test.i
    
  • If you typed echo ~userid on TerpConnect, the shell would expand ~userid to the full path of the user's home directory, if I typed echo ~, the shell would expand ~ to the full path of my own home directory:
    > echo ~
    /home/userid
    
    > echo ~userid/pub
    /home/userid/pub
    
  • Note: It is recommended that you use ~ to refer to your home directory or ~user to refer to another users home directory rather than using the full path name, since your home directory may be moved to another directory, and ~ will always refer to the proper place.
How do I:
How are we doing? Comments on this page?
Office of Information Technology
Office of Information Technology Help Desk Web Site University of Maryland Web Site Office of Information Technology Web Site