How to Locate Executable Files Using the which Command in Linux

When you type a standard command like python or docker into a Linux terminal, you are not actually executing a magical word; you are commanding the kernel to execute a highly specific binary file located somewhere on your hard drive. If you have multiple versions of Python installed simultaneously (e.g., Python 2 and Python 3), simply typing python might launch the completely wrong version, instantly breaking your development environment. To determine exactly which physical binary file the kernel is executing when you type a command, you must use the which command.

How the which Command Works

When you type a command, the Linux kernel does not search your entire 1-Terabyte hard drive to find it (that would take minutes). Instead, it rapidly scans a highly specific list of authorized directories known as the $PATH variable (which usually includes folders like /usr/bin and /usr/local/bin). The which command performs this exact same rapid scan, but instead of executing the program, it simply prints the absolute file path to the screen.

To find out exactly where your system’s Python binary is located, type:

which python

The system will instantly output a single line of text, such as:

/usr/bin/python

Diagnosing PATH Conflicts

The which command is the ultimate diagnostic tool for resolving software installation conflicts.

Imagine you just downloaded a brand new, highly customized version of the ffmpeg video encoding tool, and you manually placed the binary inside your personal /home/user/bin/ directory. However, when you type ffmpeg, the server continues to use the old, outdated version that was installed months ago.

If you run which ffmpeg, the output will immediately reveal the problem:

/usr/bin/ffmpeg

Because the /usr/bin/ directory is listed earlier in the kernel’s internal $PATH variable, the system finds the old version first and immediately stops searching. It never even looks inside your personal folder. To fix this, you must either explicitly type out the absolute path (/home/user/bin/ffmpeg) every single time, or modify your .bashrc file to reorder the hierarchy of the $PATH variable.

Get the best tech tips delivered straight to your inbox.

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