📍 Command: which (Locate an Executable)
The which command is a simple utility used to identify the location of an executable file associated with a given command. When you type a command like java or python, your shell looks through a list of directories (the PATH) to find it; which tells you exactly where it found it.
1. The "Why"
For an Arch Linux power user and developer, which is a vital diagnostic tool.
- Version Verification: If you have multiple versions of Java or Python installed,
whichconfirms which one the terminal is actually running. - Alias Detection: It helps you see if a command is pointing to a binary file or if it's been redefined as an alias in your
.bashrcorzshrc. - Troubleshooting: If a script you wrote is failing because it "can't find command," use
whichto find the absolute path to put in your shebang (e.g.,#!/usr/bin/python). - Environment Setup: Verify that your Arch packages (like
hyprlandorsupabase) are correctly installed and accessible in your environment.
2. How it Works
which scans the directories listed in your $PATH environment variable in order from left to right. It returns the path of the first matching executable it finds.
3. Basic Syntax
which [COMMAND_NAME]
4. Practical Examples for Your Workflow
A. Locating your Java Compiler
To find out where your JDK is installed:
which javac
Output might be: /usr/bin/javac
B. Checking for Multiple Versions (-a)
Sometimes a command exists in multiple places. Use -a (all) to see every location found in your PATH:
which -a python
This is helpful if you are trying to find out why a specific version of a library isn't loading.
C. Identifying Aliases
If you have an alias like alias ls='ls --color=auto', which will show you the alias definition instead of the path (depending on your shell):
which ls
D. Silently Checking for Installation
In scripts for your tutorials, you can use which to check if a tool is installed without printing the path to the screen:
if which hyprctl > /dev/null; then
echo "Hyprland tools are installed."
fi
5. which vs. whereis vs. type
| Command | What it shows | Best For |
|---|---|---|
which |
The path of the executable in your PATH. | Finding out what runs when you type a command. |
whereis |
The binary, source, and manual page locations. | Finding documentation and source files. |
type |
Tells you if a command is a binary, an alias, or a built-in shell function. | Understanding how the shell interprets a command. |
6. Pro-Tips
- Arch Linux Context: On Arch, almost all system binaries are stored in
/usr/bin/. You'll notice that/binand/sbinare actually symlinks to/usr/bin/.whichwill reflect this structure. - Absolute Paths: If
whichreturns nothing, the program is either not installed or its directory isn't in your$PATH. You'll need to update your.bashrcor.zshrc. - Development Tip: If you're using VS Code for your Android/Java work and a plugin isn't working, it often asks for the "executable path." Run
which [tool]in your terminal to get the exact string to paste into the settings.
7. Summary Reference
| Goal | Command |
|---|---|
| Find command path | which [command] |
| Find all occurrences | which -a [command] |
| Check for tool in script | which [cmd] > /dev/null |