Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Finding Files

In Chapter 15, “Help Please,” you learn about the commands whereis and which, which can be used to find programs and other system-related files in specific locations. UNIX also provides other tools so that you can search files with more discrete criteria.

In this section, you’ll learn how to use the find and locate commands.

Finding Files by Name: find

The UNIX find command is a hard one to master. It provides a zillion options that can intimidate many users. However, with a little instruction, you can learn how to use find for 90% of your needs. The basic syntax for the find command is

     find paths expression

The find command searches the paths provided for a file that matches expression. You can search multiple paths for a file, because the expression list doesn’t start until find locates an argument that begins with a dash (-).

The most basic of options is –name. It tells find that this is the name of the file you are searching for. This option is much more powerful if you use wildcards (*, ?, and []) to specify the name. (See the section “Regular Expressions” in Chapter 14, “Putting the Shell to Work.”)

The -print option tells find to print any matches to the terminal. This is optional under Linux, but some versions of find in other UNIX systems require it because by default, find does not print any results. Here’s a simple example:

      [alberto@digital alberto]$ find /home/alberto /tmp -name “*.txt” -
     print
     /home/alberto/intro.txt
     /home/alberto/Desktop/Trash/intro.txt
     /home/alberto/.netscape/cache/0F/cache3603EFAF0010427.txt
     /home/alberto/intro2.txt
     /home/alberto/kdetools.txt
     /home/alberto/Hello World.txt
     /home/alberto/test/test.txt
     /home/alberto/test/todo.txt
     /home/alberto/file.txt
     /home/alberto/file3.txt
     /home/alberto/file1.txt
     /home/alberto/file2.txt
     /home/alberto/afile.txt
     find: /tmp/.XF86Setup5909: Permission denied
     find: /tmp/kfm-cache-0: Permission denied
     find: /tmp/kfm-cache-100: Permission denied
     find: /tmp/.XF86Setup6565: Permission denied
     find: /tmp/otherMail: Permission denied
     find: /tmp/.XF86Setup514: Permission denied
     /tmp/file.txt

In this example, I want to find all files inside /home/alberto or /tmp that have a name ending with .txt. Note that I enclosed the expression in quotation marks to avoid having the shell glob (interpret) and expand the star itself. Notice that in the results, find was not able to search the directories to which I didn’t have privileges.

Although the regular expression helps in finding matches, it is still case sensitive; files with a .TXT extension are not found. Replacing the -name option with -iname makes the search case insensitive, so files ending in both .txt and .TXT are found:

     [alberto@digital alberto]$ find /home/alberto /tmp -iname “*.txt” -
      print
     /home/alberto/intro.txt
     /home/alberto/Desktop/Trash/intro.txt
     /home/alberto/FILE.TXT
     /home/alberto/.netscape/cache/0F/cache3603EFAF0010427.txt
     /home/alberto/intro2.txt
     /home/alberto/kdetools.txt
     /home/alberto/Hello World.txt
     /home/alberto/test/test.txt
     /home/alberto/test/todo.txt
     /home/alberto/file.txt
     /home/alberto/file3.txt
     …

The find command provides a myriad of options, including the capability to search for files by modification and access date. In addition, find is able to execute commands using the files found as arguments to a command. This is done with the -exec. option. Here’s an example:

     [alberto@digital alberto]$ find /home/alberto -iname “*.txt” -exec wc
      {} \;
          22     329    1896 /home/alberto/intro.txt
          22     329    1896 /home/alberto/Desktop/Trash/intro.txt
           0       0       0 /home/alberto/FILE.TXT
          10      50     785
      /home/alberto/.netscape/cache/0F/cache3603EFAF0010427.txt
          44     329    1894 /home/alberto/intro2.txt
         197     330    2403 /home/alberto/kdetools.txt
           1       1       8 /home/alberto/Hello World.txt
           0       0       0 /home/alberto/test/test.txt
           0       0       0 /home/alberto/test/todo.txt
           0       0       0 /home/alberto/file.txt
           0       0       0 /home/alberto/file3.txt
           0       0       0 /home/alberto/file1.txt
           0       0       0 /home/alberto/file2.txt
           0       0       0 /home/alberto/afile.txt

Notice in this example that after calling the wc command, there are a pair of braces ({}). These braces are replaced by the name of the file matching the search—which then becomes the argument to the wc command. Also note that at the end there’s a semicolon (;). It tells find that the -exec option is completed. To avoid having the shell interpret the semicolon as a command group, the semicolon is followed with a backslash (\).


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.