4.7. 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. 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:

find path pattern

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.

find . -name foo

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:

locate pattern

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. 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.

4.7.3. Which, whereis, whatis

which

The format of the which command is:

which command

which returns the location of binary, or executable, shell commands. The information provided by whichis useful for creating application launchers. Refer to Section 2.2.1 Adding Application Launchers for more information.

which gedit

The above command returns the information /usr/bin/gedit.

whereis

The format of the whereis command is:

whereis command

The following command returns the locations of: the binary of find, the location of the source code, and the location of the find man page.

whereis find
/usr/bin/find /usr/share/man/man1p/find.1p.gz /usr/share/man/man1/find.1.gz
whatis

The format of the whatis is:

whatis command

This command returnsinformation about the command from each of its man pages.

whatis lp

The above command returns:

lp          (4) - line printer devices 
lp(lp-cups) (1) - print files

TipTip
 

Checking the usage of a command using whatis before reading the man page can save you some time.