DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Directories and files

Narrowing the listing: using wildcards

You have seen in the examples in this chapter that sometimes a UNIX system directory has so many files that listing the directory fills more than a screen. If you have some idea of the files you are looking for, you can narrow your search using wildcard characters:

   $ cd /bin
   $ lc c*
   cal     cb      chgrp   chown   cmchk   comm    cp      csh
   cat     cc      chmod   chroot  cmp     copy    cpio    csplit
A wildcard character takes the place of another character or characters. They are also known as metacharacters, because they have a meaning beyond that of a single, regular, character. In the example above, the ``*'' is a metacharacter, so the command reads: ``list all files starting with a ``c'', followed by any other character or characters.'' Metacharacters are interpreted by the shell, rather than by commands.

Here are the filename metacharacters:

Metacharacter Means
* Any character or characters, including no characters at all
? Any single character
[...] Any enclosed character; specify a range with ``-''; for example, to match file.a, file.b or file.c, you could use file.[a-c]

Here are some more examples:

   $ cd /etc
   $ lc [cde]*
   checklist   cron        custom    devnm    dmesg       ext.perms
   clri        cshrc       ddate     divvy    dsmd.perms
   cmos        cshrc.bak   debrand   dkinit   emulator
   

default: archive cc format lock micnet passwd tape.00 archive- cron goodpw login- mkuser passwd- tar backup dumpdir idleout login mkuser- restor usemouse boot dumpsrv imagen lpd msdos su xnet boothd filesys lang mapchan netbackup tape

   $ cd /etc
   $ lc [c-e]*
   checklist   cron        custom    devnm    dmesg       ext.perms
   clri        cshrc       ddate     divvy    dsmd.perms
   cmos        cshrc.bak   debrand   dkinit   emulator
   

default: archive cc format lock micnet passwd tape.00 archive- cron goodpw login- mkuser passwd- tar backup dumpdir idleout login mkuser- restor usemouse boot dumpsrv imagen lpd msdos su xnet boothd filesys lang mapchan netbackup tape

Both the first and the second example list all the files in /etc beginning with a ``c'', ``d'', or ``e'' and followed by any other characters, but the second example uses a range [c-e] to do it.
   $ l /etc/q?
   l: /etc/q? not found: No such file or directory (error 2)
In the third example, l /etc/q? does not produce a list of files because the computer is looking for a file in /etc that begins with a ``q'' and has just one other character following it; this does not match any of the files in /etc. (C shell users would see the message No match.)

With the ``?'' metacharacter, you must type as many ?s as there are letters in the filename you want to match. For example, to search for a six-character filename in /etc directory which begins with ``pa'', enter:

   $ l /etc/pa????
   -r--r--r--   1 root     techpubs    2968 Jun 19 15:28 /etc/passwd

Try using a metacharacter to find the message of the day file:

  1. Type cd /etc and press <Enter>.

  2. Type l mo* and press <Enter> to see a long listing of all the files beginning with ``mo'' in /etc. (Your screen should look something like the following screen display.) /etc/motd is the message of the day file.
    $ cd /etc
    $ l mo*
    -rw-r--r--   1 root     sys          111 Nov  3 02:34 motd
    -rwx--x--x   2 root     bin        27564 Jan  5 04:53 mount
    -rwx------   1 root     bin         1071 Nov  3 02:22 mountall
    -rwx--x--x   1 root     root       23180 Nov  3 02:32 mountd
    

Next topic: Summary
Previous topic: What you see in a long listing

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003