3.6. Locating Files and Directories

There may be times when you know a file or directory exists but you do not know where to find it. To search for a file or directory, use the locate command.

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:

locate finger

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.

TipTip
 

As a daemon, cron executes tasks at regularly scheduled intervals. To read the cron man page, type man cron at the shell prompt.

The cron task periodically updates the slocate database, which is used to catalog file locations. Switching between operating systems and shutting down your machine at the end of the day can interfere with the automatic database update run by 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.

NoteNote
 

You can run anacron to have your system execute commands periodically, with a frequency specified in days. Unlike cron, it does not assume that the machine is running continuously. Hence, it can be used on machines that are not running 24 hours a day to control daily, weekly, and monthly jobs that are usually controlled by cron.

Refer to the man page on anacron (type man anacron at the command line) and the Red Hat Enterprise Linux System Administration Guide for more information.