The where command in Linux helps you locate the full path of executable files by searching through directories listed in your PATH environment variable. It's useful for verifying which version of a command will run when you type it, especially when multiple versions exist. No special tools are needed—just a terminal and basic shell access.
What You'll Need
- A Linux terminal (bash, zsh, sh, etc.)
- User access to run commands (no root privileges required)
- The 'where' command available (typically part of the util-linux package)
Step-by-Step
- Open your terminal emulator (e.g., GNOME Terminal, Konsole, or xterm).
- Type 'where' followed by the name of the command you want to locate, such as 'where ls', and press Enter.
- Review the output: each line shows a full path where an executable matching the name was found in your PATH directories.
- If no output appears, the command is not found in any directory listed in your PATH—check spelling or install the program.
- To search for all matches (not just the first), use 'where -a command_name'—this lists every instance found.
- Combine with other commands: for example, 'where python' then use the path to check version with '/usr/bin/python3 --version'.
- Note that 'where' only searches PATH—it does not scan the current directory unless '.' is included in PATH (not recommended for security).
- Assuming 'where' searches the entire filesystem—it only checks directories in the PATH variable, not /home, /root, or arbitrary folders.
- Confusing 'where' with 'which'—while similar, 'where' (from util-linux) can show multiple paths and is more consistent across distributions than the 'which' script.
- Expecting to find shell built-ins like 'cd' or 'alias'—these are not files and won't appear in 'where' output.
- Using 'where' without arguments and expecting help—it requires a command name; use 'where --help' for usage.
- Believing 'where' finds scripts in the current directory—unless '.' is in PATH (unsafe), it won't; use './script.sh' to run local scripts.
- Use 'where -a' to see all versions of a command—helpful when troubleshooting PATH conflicts or multiple installations (e.g., Python 2 vs 3).
- Check your PATH with 'echo $PATH' if 'where' returns nothing—ensure the directory containing the executable is included.
- Combine with 'ls -l' to inspect permissions: 'ls -l $(where cmd)' shows if the file is executable.
- Use in scripts to verify dependencies before running: 'if ! where -s dependency; then echo "Missing"; fi'.
- Remember that 'where' follows symlinks and shows the real path—useful for checking if a command points to a different version via a link.
- On minimal systems, install 'where' via your package manager: e.g., 'sudo apt install util-linux' on Debian/Ubuntu.
- If 'where' returns 'command not found', first verify the util-linux package is installed—try 'dpkg -l | grep util-linux' or 'rpm -q util-linux'.
- If the command exists but 'where' doesn't find it, check that its directory is in PATH—temporarily add it with 'export PATH="/new/dir:$PATH"' for testing.
- If you get unexpected output, ensure you didn't typo the command name—'where ls' vs 'where sl' (the latter may not exist).
- If 'where' seems slow, it's likely scanning many PATH directories—consider simplifying PATH by removing unused or duplicate entries.
- On some minimal containers, 'where' may be missing—use 'which' or 'command -v' as alternatives, though they may not show all matches.
- If you suspect a hijacked PATH (security concern), audit it with 'echo $PATH | tr ':' '\n'' and review each directory for legitimacy.
Common Mistakes
Tips
Troubleshooting
Frequently Asked Questions
Does 'where' work the same on all Linux distributions?
Yes, as long as the util-linux package is installed, which is standard on most distributions including Ubuntu, Debian, Fedora, and Arch.
Can 'where' find aliases or shell functions?
No—it only locates executable files in directories listed in PATH; aliases and functions are handled by the shell and not stored as files.
Is 'where' better than 'which'?
In many cases, yes—'where' from util-linux is more reliable, shows all matches by default with -a, and is less prone to variation across systems than the 'which' script.
Why does 'where' return nothing for a command I know exists?
Most likely, the directory containing the executable is not in your PATH variable—check with 'echo $PATH' and adjust if needed.
Can I use 'where' in shell scripts safely?
Yes—it's portable across Linux systems with util-linux installed; for maximum portability, consider 'command -v' as a POSIX alternative, though it shows only one match.
Does 'where' search the current directory by default?
No—it only searches PATH; to include the current directory, you would need to add '.' to PATH, which is discouraged due to security risks.