How to Find the Path of a Command in Ubuntu Linux Terminal Using the which Command

When you type a command like python3, nano, or grep into your Ubuntu terminal and press Enter, Linux instantly executes the program. But how does it know where that program is located on your hard drive? The operating system searches through a specific list of directories (known as your PATH variable) to find the executable file. Sometimes, especially when installing new software or managing multiple versions of a programming language, you need to know exactly which executable file is actually running. The which command is the perfect tool for identifying the absolute location of a command on your file system.

How the which Command Works

The which command searches your current PATH environment variable for the name of the executable you specify. It does not search your entire hard drive (like the find or locate commands do). It only looks in the specific folders where Linux expects executable programs to live (such as /bin, /usr/bin, or /usr/local/bin).

Step 1: Use the which Command

Using the command is incredibly simple. You just type which followed by the name of the command you want to locate.

  1. Open your terminal (Ctrl + Alt + T).
  2. Type which followed by a space, and then the command name. For example, to find out exactly where the Python 3 interpreter is located, type:
    which python3
  3. Press Enter.
  4. The terminal will print the absolute file path on the next line. For example, it might return:
    /usr/bin/python3

This tells you that when you type python3, the system is executing the file located at /usr/bin/python3.

Step 2: Understanding “Not Found” Errors

If you type a command and the terminal simply returns to a new prompt without printing a path, it means one of two things:

  1. The program is not installed on your computer.
  2. The program is installed, but the folder containing it is not listed in your system’s PATH variable. Therefore, Linux does not know it is an executable command.

For example, if you type which imaginarycommand, the terminal will return nothing.

Step 3: Finding Multiple Instances (-a Flag)

Sometimes, you might have multiple versions of the same program installed in different directories. By default, the which command stops searching as soon as it finds the very first match in your PATH. This first match is the one that actually runs when you type the command.

If you want to see every location where that executable exists within your PATH, use the -a (all) flag.

  1. In your terminal, type:
    which -a java
  2. Press Enter.
  3. If you have multiple Java environments installed, the terminal might return several lines, such as:
    /usr/bin/java
    /usr/local/java/bin/java

This is extremely helpful for troubleshooting situations where an older version of a program is running instead of the new version you just installed.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.