There may be times when you know a file or directory exists
but you do not know where to find it. There are several
commands you can use to search for it, including
find, locate, and
which.
4.7.1. Find
The format of the find command is:
If you do not specify a path, find starts in the current
working directory and looks through all subdirectories for the
specified pattern.
The find command has many options that you can review by
entering man find at a shell prompt.
The most common option is -name, which
tells find to search for all files and directories with a
certain string of letters in their name.
The above command searches through the current working
directory for all files with "foo" in their name.
4.7.2. Locate
The format of the locate command is:
With locate, you can see every file or
directory whose name contains the search criterion. For
example, to search for all files with the word
finger in the name, type:
The locate command uses a database to
locate files and directories that have the word
finger in the file or directory name. The
search results could include a file called
finger.txt, a file called
pointerfinger.txt, a directory named
/fingerthumbnails/, and so on. To learn
more about locate, read the
locate man page (type man
locate at a shell prompt).
The locate command works very quickly,
as long as the database is up to date. That database is
automatically updated on a nightly basis through a
cron job. cron is a
small program that runs in the background, performing various
tasks (such as updating the locate
database) at regularly scheduled intervals. Refer to the Red Hat Enterprise Linux System Administration Guide for more information on cron.
To update the database manually, log in as root (type
su - at a shell prompt and then your root
password) and type the command updatedb.
After a few minutes, the slocate
database that is used by the locate command
is updated.
When you are done working as root, type
exit at the prompt; you are returned to
your user account.