When you type a standard command like python3, curl, or nano into the Ubuntu terminal and press Enter, the system instantly executes the program. It does this because Ubuntu maintains a master list of directories (known as the `$PATH` variable). Whenever you type a command, the operating system rapidly scans those specific directories until it finds the executable file that matches the word you typed.
However, if you are troubleshooting a complex server setup, managing multiple versions of a programming language, or writing a bash script that requires absolute file paths, you cannot simply guess where a program is installed. You need to know exactly which folder the executable binary lives in.
Using the ‘which’ Command
The fastest and most reliable way to locate a command’s executable file is by using the which command. This built-in utility asks the system, “If I type this command right now, which exact file are you going to run?”
- Open your terminal.
- Type
whichfollowed by a space, and then the name of the command you are looking for. For example, to find out where the Python 3 interpreter is installed, type:
which python3
- Press Enter.
The terminal will instantly output the absolute path to the executable file, such as:
/usr/bin/python3
This tells you definitively that the python3 binary is located in the /usr/bin/ directory.
Why This is Important: Multiple Installations
The which command is critical for solving “wrong version” errors. It is entirely possible to have two different versions of the same software installed on a server simultaneously.
For example, Ubuntu might include an ancient version of Python in /usr/bin/python, but you might have manually installed a brand-new version into a different directory like /usr/local/bin/python. When you type python, which one actually runs?
By running which python, the system will tell you exactly which path it prioritizes, allowing you to instantly determine if your scripts are accidentally using the outdated software instead of your newly installed version.
The ‘whereis’ Command (The Broader Search)
If you need more information than just the executable binary, you can use a secondary tool called whereis.
whereis python3
Instead of just printing the path to the executable, whereis will output a much longer string. It will show you the location of the executable, the location of the source code (if available), and the location of the manual pages (the documentation) for that command.