How to Use the which Command in Linux, With Examples

Linux which Command

This tutorial will teach you how to use the which command in Linux with some simple examples.

The which command will tell you the path to the executable used by a command on the system if it exists.

Why is this useful? Say you’ve got two copies of the MySQL executable installed on your system (installed via different means), and you want to know which one is actually in use so that the other can be removed – the which command can tell you which of the two is called when you execute MySQL on the command line.

Linux which Command Syntax

The command for the which command is as follows:

which OPTIONS COMMAND

Note that:

  • OPTIONS is an optional list of options from the below table which can be used to change the default behavior of the which command
  • COMMAND is the name of the command you wish to know the path to the executable for
    • The COMMAND must be supplied, or which will have nothing to look for!

Command Options

Here are the most commonly used options which can be passed to the which command:

–all, -a Print all matching executables in PATH, not just the first.
–read-alias, -i Read aliases from stdin, reporting matching ones on stdout. This is useful in combination with using an alias for which itself.
–skip-alias Ignore option –read-alias’, if any. This is useful to explicitly search for normal binaries, while using the –read-alias’ option in an alias or function for which.
–read-functions Read shell function definitions from stdin, reporting matching ones on stdout. This is useful in combination with using a shell function for which itself.
–skip-functions Ignore option –read-functions’, if any. This is useful to explicitly search for normal binaries, while using the –read-functions’ option in an alias or function for which.

As always, you can view the full user manual for the which command by running:

man which

which Command Examples

Below, we look for the path for the executable, which is called when nano is run from the terminal:

which nano

This will return something like the following if the nano text editor is installed on your system:

/usr/bin/nano

If nano is not installed, you’ll see:

nano not found

This makes the which command useful for both checking where an executable is located and whether a package is installed/available or not.

Multiple executable names can be supplied if you wish to look up multiple commands at once:

which cat less

Which will return:

/bin/cat
/usr/bin/less

Return Codes

If you are using which in a Shell Script, you can use the status code returned by the command to determine whether an executable path was found or not:

0 All specified commands were found and are executable.
1 One or more of the specified commands was not found or is not executable.
2 Invalid options or syntax were used.

This can be super useful if you want to make your shell scripts more portable – you can use the which command to check that a command is available before executing it in your script, to make sure the end-user doesn’t receive any errors if they do not have the required software installed.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *