Linux Syntax Commands: The which Command

Linux Syntax Commands: The which Command
Page content

Introduction

Have you ever attempted to install an application only to have it ask you the explicit path to a particular executable file? When you have no idea how to find that executable, you wind up on a wild goose chase taking you through the /usr/bin directory, the /usr/sbin directory, and the /sbin directory. But there is a much easier way to do this…using ‘which’. The which command is simple to use, but the output can save you a good deal of time.

Basic Usage

Using the which command is simple. The basic usage is:

which COMMAND

Where COMMAND is the actual command you are searching for.

Let’s say you need to know where the aterm executable command is. To find this you would issue:

which aterm

This command should return:

/usr/bin/aterm

Only if you have installed aterm in a different location (such as /usr/local/bin) will the output of the above command vary.

Of course which does have a few arguments to use to make it even more helpful. Let’s take a look at the more useful arguments.

  • a - This prints out all matches, not just the first.
  • i - This will include any user-created aliases found in your .bashrc file.
  • --skip-alias - This will skip your aliases. This is built into the default command.
  • --skip-dot - This will skip any directory that is hidden (starts with a “.”).
  • --skip-tilde - This will skip any directory that starts with a “~”.

Gotcha

Because which only searches the users’ PATH variable it will not find an executable that is not there. For instance: If a standard user issues the command:

which zcip

the return will look like:

which: no zcip in (/usr/local/bin:/bin:/usr/bin:/usr/games:/usr/lib/qt4/bin:/usr/bin:/opt/kde4/bin:/home/jlwallen/bin:/usr/lib/qt4/bin:/usr/bin:/opt/kde4/bin)

As you can see, which did not find zcip in the users’ PATH. This is because zcip is in /sbin. If you issue the same command as the root user you will see:

/sbin/zcip

because this is in the root users’ PATH.

Final Thoughts

You may not use the which command very often, but when you do you will quickly see it’s value. There are many reasons you may need to know the explicit path of an executable file. When you do, which is there to help.

This post is part of the series: Linux configuring and programming tools

Linux can be a bit overwhelming as a new user. Fortunately there are plenty of tools to make this easier. In this Bright Hub Linux syntax commands series you will be introduced to various tools that will aid in the configuration and programming process.

  1. Linux Configuration and Programming tools
  2. Linux Command Line: g++
  3. Linux Command Line: which
  4. Linux Command Line: diff