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

When you type a command like python or nano into the Linux terminal, the operating system executes the program flawlessly. However, have you ever wondered where that program actually lives on your hard drive?

Linux does not magically know where every program is located. Instead, it searches through a predefined list of directories (known as your $PATH) until it finds an executable file that matches the command you typed.

If you are writing a shell script, diagnosing why the wrong version of Python is launching, or trying to understand where a newly installed software package placed its files, you need to know the exact absolute path of the command. The fastest way to find this is by using the which command.

How to Use the which Command

The syntax is incredibly simple and requires no complicated flags or arguments.

Syntax: which [command_name]

For example, if you want to know exactly where the Python interpreter is located, you would open your terminal and type:

which python3

Press Enter, and the terminal will output the absolute path to the executable file, such as:

/usr/bin/python3

This tells you that when you type “python3”, Linux is executing the specific file located in the /usr/bin/ directory.

How the which Command Works

It is important to understand that the which command does not search your entire hard drive (unlike the find command). Searching the entire drive would take several minutes.

Instead, which mimics exactly what the terminal does when you type a command: it only looks inside the directories listed in your environmental $PATH variable (which usually includes folders like /bin, /usr/bin, /usr/local/bin, etc.).

It reads those directories from left to right and stops the moment it finds a match. This is crucial for troubleshooting.

Troubleshooting Multiple Versions (The -a Flag)

A very common problem in Linux development is having multiple versions of the same software installed. For example, your system might have a default version of Java installed in /usr/bin/java, but you manually downloaded a newer version and put it in /usr/local/java/bin/java.

If you run standard which java, it will only return the first one it finds in the path. It will ignore the second one.

To force the which command to print out every instance of an executable it can find in your path, use the -a (all) flag.

which -a java

This will output a list of all matching executables, showing you exactly what versions are available on your system, helping you diagnose conflicting software installations.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

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

Receive our best articles and tips delivered straight to your inbox.