Finding files in Unix

There are several ways of finding a file on a Unix system. The methods are listed in decreasing order of generality and increasing order of speed. Not all of them exist on all systems. A common usage is given here, but do look at the man-page for more information.

find

This searches recursively from a specified directory and can be used to find any file:

        find <location> -name '<name>'

eg

        find . -name '*.txt'

Here '.' means the current directory. Also note the use of quotes to prevent the '*' from being interpreted by the shell. Use -iname for a case insensitive search.

locate

This relies on a database of filenames that may not update very often (and may not exist). It is very fast when it does work:

        locate <filename>

If you get too many results, using grep may help.

whereis

This looks in some standard directories for the specified program:

        whereis <program_name>

You may get more than one result if you have more than one copy installed.

which

This returns the full path of the command that is run when you type in a command:

        which <program_name>

NB: this is very useful when you need to know what to put on the #! line of interpreted scripts.