previous contents up next

Unix for Advanced Users

5. Navigating Effectively

5.1. Where is that command?

It often happens that you are logged onto an unfamiliar machine, or using a different Unix implementation from the one you use most often. In this scenario, it is common for files to be in what seem like strange places. Different versions of Unix put the same standard files in different directories, and different sysadmins have different preferences about where to install extra software.

5.1.1. whereis

Usually, a command will be in one of a handful of common directories: /bin/, /sbin/ /usr/local/bin, /opt/bin, /usr/ucb (under Solaris) or /usr/bsd (under Irix) for BSD-style commands, et cetera. Rather than look in all these directories yourself, it would be nice to have a command to do it for you. Better yet would be a command that knows all the standard locations for programs in the operating system you are using, so you would be spared the work of remembering all the quirks of the different Unixes.

Fortunately, there is such a command. It is called whereis. It will search in the right places not only for executables but also for libraries, man pages and configuration files. Once it finds a matching file, it will print out the complete path to it. Normally you want to add the directory part of the path to your shell's path or PATH variable, so that you can invoke the command next time without typing its full path.

If you do not know where whereis is, things become slightly more complicated.

5.1.2. which

What if you know a command and can run it, but you need to know its directory? Then you are better off using which. Rather than search all the usual system directories, which searches only those directories in your PATH or path environment variable. It returns the full path to the program that you are using. which netscape is often a useful command, since Netscape is third-party software and hence tends to be installed in different places by different people. Its output looks like this:
/usr/local/bin/netscape
--just the information you needed.

which is especially useful when there are two commands with the same name in different directories of your path. This is a frequent occurrence on systems that have both BSD-style and System V-style commands. If you find yourself typing a command that you know is right and being told your syntax is faulty, it may be that you are using System V syntax with the BSD version of the command, or vice versa. For example, say you type 'ps aux' on a Solaris machine, and receive a message like this:

usage: ps [ -aAdeflcj ] [ -o format ] [ -t termlist ] [ -u userlist ] [ -U userlist ] [ -G grouplist ] [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] 'format' is one or more of: user ruser group rgroup uid ruid gid rgid pid ppid pgid sid pri opri pcpu pmem vsz rss osz nice class time etime stime f s c tty addr wchan fname comm args
This is the system's way of dissing you. It is telling you the correct syntax to the command (in exhaustive detail), implying that the syntax you used was faulty. Fortunately, you know about the difference between System V- BSD-style commands, so rather than wallow in soul-searching feelings of personal unworthiness, you do 'which ps'. The reply is
/usr/bin/ps
. This being a Solaris system, you want /usr/ucb/ps, the University of California at Berkeley version of the command. Placing /usr/ucb before /usr/bin in your PATH or path gives you the familiar syntax you wanted in the first place, so now typing ps aux works fine.

People often combine which and whereis. If you did not know about /usr/ucb, a directory that exists only under Solaris, you might try whereis ps after your first, failed attempt to do ps aux. The reply would be

ps: /usr/bin/ps /usr/ucb/ps /usr/man/man1/ps.1 /usr/man/man1b/ps.1b
Ignoring the man pages, you notice that there are two executable binaries (files in bin/ directories) called ps. Now you can ask which ps, and find that /usr/bin/ps is the one you are using. Since it does not work as expected, you try the other one, /usr/ucb/ps. It works. You have found the command you wanted.

previous contents up next