Datasheet

Task 1.4: Find Files
21
Duration
This task should take half an hour or an hour to complete. Once you’ve mastered these com-
mands, you should be able to search for files in a matter of seconds—although some of these
commands may take several minutes to execute.
Setup
No special setup is required. Although the commands used in this task may be used by either
root or ordinary users, some of them work better when run as root because root may exam-
ine the contents of any directory, whereas ordinary users may not. Thus, you should use su to
acquire root privileges for this task.
Caveats
If you run this task as root, be careful what you type. Although the search commands them-
selves are non-destructive, you should be sure not to mistype a command and make it some-
thing destructive.
Procedure
To perform this task, you’ll search for the whatis program file using each of the three file-
location commands in turn. In the process, you’ll learn the capabilities and limitations of each
of these commands.
Using find
The find utility implements a brute-force approach to finding files. This program finds files
by searching through the specified directory tree, checking filenames, file creation dates, and
so on to locate the files that match the specified criteria. Because of this method of operation,
find tends to be slow, but it’s very flexible and is very likely to succeed, assuming the file for
which you’re searching exists. To search the entire computer for the whatis program file by
name, type the following command:
# find / -name whatis
This command specifies the path to search (/, meaning the entire directory tree) and the cri-
teria to use for the search (-name whatis, to search for a file whose name is whatis). Because
this command searches the entire directory tree, it’s likely to take a long time to complete—
perhaps several minutes. This command is guaranteed to find any file meeting the specified cri-
teria, though, at least assuming you have permission to read the directory in which it resides
and don’t make a mistake when specifying the criteria. The output of the find command is a
list of files that match the specified criteria, one file per line.
If you’re not certain of the exact filename, you can use wildcards in the filename specifica-
tion, as in -name “what*” to search for any file whose name begins with what. Using quotes
around the search specification ensures that the shell will pass the wildcards to the find com-
mand rather than try to expand the wildcard itself.
83484.book Page 21 Monday, September 18, 2006 8:58 AM