previous contents up next

Unix for Advanced Users

5. Navigating effectively

5.2. Where is that file?

If you are looking for a particular file on a system, there are two distinct cases to consider.

5.2.1. whereis

The first is that it is part of an installed program --an executable, a library, a man page, or a configuration file--that can be found in a standard place. In that case, the command to use is whereis. (If you do not know where whereis is, things become slightly more complicated.) whereis queries the standard directories for software that may be shared by all users, and finds any files that contain the string passed as its argument. whereis passwd, for instance, gives the following output (on a Linux system):
passwd: /usr/bin/passwd /etc/passwd /usr/local/bin/passwd /usr/man/man1/passwd.1 /usr/man/man5/passwd.5
The first and third entries are executables, since they live in subdirectories named bin. The second must be a configuration file, since it resides in /etc. The fourth and fifth are obviously man pages. (In fact, some knowledge of the meaning of section numbers shows that the first man page is for a user command, which will be either /usr/bin/passwd or /usr/local/bin/passwd, and that the second is for a configuration file, probably /etc/passwd. To find out definitively, one can just read the man page.)

5.2.2. find

What if the file does not belong to a program installed system-wide? What if it instead belongs to a user? It probably does not own any files in system directories like /usr/bin or /etc. In this case, the find command is more useful.

find is an extremely rich command; it can be told to look for files with certain modification dates, or to match regular expressions in files' contents, or to perform an operation on a file. All these features are documented in the find man page.

For normal purposes, though, all you want to do is to find a file by name. In that case, the syntax is very simple: find directory -name pattern. Here, directory is a path like /usr/local and pattern is either a simple string or a wildcard expression of the same type as is used by the shells. find /tmp -name '*lock*', for instance, will list all the files in the directory tree under /tmp whose names contain "lock" (so, e.g., the files named schlock, interlocking.dat and lockergnome would all be found). N.B.: In the above example, the single quotes (' and ') around the wildcard expression are mandatory. Without them, the expression would be expanded by the shell. Then it would match against the contents of the current directory, instead of being passed "as is" to find.

previous contents up next