Academic Computing Services * Simon Fraser University


Use Unix Wildcards and Link Files

© August 25, 1995 B-8

Many computer operating systems provide ways to select certain files without typing complete filenames. For example, you may wish to remove all files whose names end with "old". Unix allows you to use wildcards (more formally known as metacharacters) to stand for one or more characters in a filename.

The two basic wildcard characters are ? and *. The wildcard ? matches any one character. The wildcard * matches any grouping of zero or more characters. Some examples may help to clarify this. (Remember that Unix is case-sensitive). Assume that your directory contains the following files:

Chap                 bite

Chap6                it

Lit                  site

big                  snit


bin.old              test.old


The ? wildcard

The command ls will list all the files (see how-to B-5, List Contents and Navigate Unix Directories, for more information on ls). The command

ls ?it  


Lit    bit

lists only the files Lit and bit. The file snit was not listed because it has two characters before "it". The file it was not listed because it has no characters before "it".

The ? wildcard may be used more than once in a command. For example,

ls ?i?


Lit     big      bin     bit

finds any files with "i" in the middle, one character before and one character after.

The * wildcard

The * wildcard is more general. It matches zero or any number of characters, except that it will not match a period that is the first character of a name.

ls *it


Lit     bit     it     snit

Using this wildcard finds all the files with "it" as the last two characters of the name (although it would not have found a file called .bit).

We could use this wildcard to remove all files in the directory whose names begin with "test". The command to do this is

rm test*

See how-to B-4, Create, Copy, Rename and Remove Unix Files and Directories, for more on rm.

Be careful when using the * wildcard, especially with the rm command. If you had mistyped this command by adding a space between test and *, Unix would look first for a file called test, remove it if found, and then proceed to remove all the files in the directory! (How-to B-12, Record, Reissue and Rename Unix Commands, describes how to make Unix ask for confirmation before removing any files.)

Matching a range of characters with [ ]

The ? wildcard matches any one character. To restrict the matching to a particular character or range of characters, use square brackets [ ] to include a list. For example, to list files ending in "ite", and beginning with only "a", "b", "c", or "d" we would use the command:

ls [abcd]ite

This would list the file bite, but not the file site. Note that the sequence [ ] matches only one character. If we had a file called delite, the above command would not have matched it.

You can also specify a range of characters using [ ]. For instance, [1-3] will match the digits 1, 2 and 3, while[A-Z] matches all capital letters.

ls [A-Z]it

will find any file ending in "it" and beginning with a capital letter (in this case, the file Lit).

Wildcards can also be combined with [ ] sequences. To list any file beginning with a capital letter, we would use:

ls [A-Z]*


Chap1    Chap6    Lit

Matching a string of characters with { }

The method described in the previous section matches a single character or range of characters. It is also possible to match a particular string by enclosing the string in { } (braces). For example, to list only the files ending in the string "old", we would use

ls *{old}


bin.old   test.old

To list all files ending in either "old" or "new", use

ls *{old,new}


bin.old  test.old

Linking files

The ln command (short for link) lets you give multiple names to a single file. This is useful when you want to get at a file quickly from within different directories. Assume that our directory structure looks like this:

We may need to refer frequently to a file called table1 in the tables subdirectory when we are in our home directory (jsmythe). Rather than typing the whole pathname


we could link the file to our home directory. If we are in the home directory, the command to do this is:

ln project1/chapter3/tables/table1 mytable1

To create the link when we are in the tables directory, the command would have been:

ln table1 ~/mytable1

(See how-to B-5, List Contents and Navigate Unix Directories, for information on using ~ as an abbreviation for the home directory).

After issuing either of these commands, an ls command in our home directory would show an entry for mytable1. The long format of the same command would show 2 links for the file mytable1:

ls -l


-rw-------  2 jsmythe   6 Jul  4 14:23 mytable1

A long format listing of the file table1 would also show 2 links. (See how-to B-5, List Contents and Navigate Unix Directories, for more information on the ls -l command.)

What if a file called mytable1 had already existed in our home directory? Unix would let you know that a file by that name exists, and would not make the link.

The effect of linking is that the file now has two names. You may call up the file by either name. Creating a link does not change the ownership, group, or permissions for a file.

Removing links

Links are removed using the rm command. To continue the example above, the command

rm mytable1

removes one link to the file table1 by removing the file mytable1. The file table1 itself, and its contents, still exists. Only when all the links to the file have been removed will the file itself be erased.

Symbolic links

The links described in the sections above are "hard" links. In effect, making a hard link creates a standard directory entry just like the one made when the file was created. Hard links have certain limitations. Hard links cannot be made to a directory, only to files, and hard links cannot be made across file systems and disk partitions.

There is another kind of link, called a symbolic link. Symbolic links can span file systems, and can be made for directories.

In the figure above, assume that we want to make a symbolic link from our home directory (jsmythe) to the directory called chapter3. To create the symbolic link, we would move to our home directory and give the command:

ln -s project1/chapter3 linkdir

The -s option means that a symbolic link is created (instead of the default hard link). This command creates a symbolic link called linkdir which points to the directory called chapter3.

When we list the contents of linkdir

ls linkdir


data     tables

we see a listing of the contents (data and tables), just as if we had listed the contents of chapter3.

We can learn more about the new directory by using the long format and directory options with the ls command:

ls -ld linkdir


l---------  1 staff  7 Jun 11 13:27 linkdir -> project1/chapter3

Symbolic links are shown with an arrow (->) in the name column at the right.

Removing symbolic links

Use rm to remove a symbolic link, for example

rm linkdir

This removes the link only, not the file or directory it was pointing to. If the file or directory is removed but the link remains, the link will no longer work.

* * * * * * * * * * * * * * *

This page written and maintained by Academic Computing Services, Simon Fraser University.
Please e-mail questions or comments to