Skip to content

which MacOS command

The which command in MacOS is used to locate the path of a specific executable file within the operating system. By typing “which” followed by the command name, you can determine the exact location of that command in the system’s directories. This can be helpful in situations where you have multiple versions of a program installed, or if you want to ensure that you are running the correct version. The which command searches through the directories listed in the PATH environment variable to find the specified command. It will display the full path to the executable file if it is found, or provide no output if the command is not located in any of the directories.

which Syntax:

Terminal window
which [option] [command]

which Options:

OptionDescription
-aPrint all instances of the command found in the user’s PATH.
-sOnly print the first instance of the command found in the user’s PATH.
-hDisplay help and exit.
—versionOutput version information and exit.

Parameters:

ParameterDescription
commandThe name of the command to be searched in the user’s PATH.

which Command Usage Examples:

Locate the Path to the ls Command

Terminal window
which ls

Displays the path to the “ls” command, which is typically used to list directory contents.

Find the Location of the Python Interpreter

Terminal window
which python

Returns the path to the Python interpreter, which is essential for running Python scripts and programs.

Identify the Directory of the Bash Shell

Terminal window
which bash

Shows the path to the Bash shell, a popular Unix shell used for executing commands and scripts.

Locate the Location of the Vim Text Editor

Terminal window
which vim

Provides the path to the Vim text editor, which is often used for text editing and programming tasks.

Find the Path to the GCC Compiler

Terminal window
which gcc

Retrieves the path to the GCC compiler, which is used for compiling C and C++ programs.

How do I use which in MacOS?

To use the which command in bash, execute the following command:

Terminal window
which ls

How can I check the location of a specific command with which?

To check the location of a specific command using which, you can simply provide the command name as an argument. For example:

Terminal window
which code

Is it possible to get the path to all occurrences of a command using which?

Yes, you can use the -a option with which to get the paths to all occurrences of a command in the order of execution priority. Here is an example:

Terminal window
which -a python

How do I display the shell built-in command with which?

To display the shell built-in command using which, you can use the -a option along with the command. For instance:

Terminal window
which -a cd

Can which be used to find the location of an alias?

No, which does not work with aliases. It only searches for executable files in the directories listed in the PATH environment variable.

How can I search for a command in a specific directory with which?

To search for a command in a specific directory using which, you can specify the directory path before the command. Here is an example:

Terminal window
which -a /usr/bin python

Is it possible to ignore aliases when using which?

Yes, you can ignore aliases by using the -p option with which. This will only search for executables and skip aliases. For example:

Terminal window
which -p ll

Applications of the which command

  • Locating the path of an executable file
  • Verifying the location of a command
  • Resolving conflicts between different versions of a command
  • Checking if a specific command is available in the system