home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

9.19. Wildcards with "Fast find" Database

locate and all the "fast find" commands I've used can match shell wildcards (Section 1.13) (* , ?, [ ]). If you use a wildcard on one end of the pattern, the search pattern is automatically "anchored" to the opposite end of the string (the end where the wildcard isn't). The shell matches filenames in the same way.

The difference between the shell's wildcard matching and locate matching is that the shell treats slashes (/) in a special manner: you have to type them as part of the expression. In locate, a wildcard matches slashes and any other character. When you use a wildcard, be sure to put quotes around the pattern so the shell won't touch it.

Here are some examples:

  • To find any pathname that ends with bin:

    % locate '*bin'
    /bin
    /home/robin
    /home/robin/bin
       ...
  • To find any pathname that ends with /bin (a good way to find a file or directory named exactly bin):

    % locate '*/bin'
    /bin
    /home/robin/bin
    /usr/bin
       ...
  • Typing locate '*bin*' is the same as typing locate bin.

  • To match the files in a directory named bin, but not the directory itself, try something like this:

    % locate '*/bin/*'
    /bin/ar
    /bin/cat
       ...
    /home/robin/bin/prog
  • To find the files in /home whose names end with a tilde (~) (these are probably backup files from the Emacs editor):

    % locate '/home/*~'
    /home/testfile~
    /home/allan/.cshrc~
    /home/allan/.login~
    /home/dave/.profile~
       ...

    Notice that the locate asterisk matches dot files, unlike shell wildcards.

  • The question mark (?) and square brackets ([ ]) operators work, too. They're not quite as useful as they are in the shell because they match the slashes (/) in the pathnames. Here are a couple of quick examples:

    % locate '????'
    /bin
    /etc
    /lib
    /src
    /sys
    /usr
    % locate '/[bel]??'
    /bin
    /etc
    /lib

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.