To display the path of an executable file in Linux, use the which, type, or command -v commands. For example, which python3 prints the full path to the Python 3 executable if it's in your PATH.

What Does "Path" Mean for an Executable?

In Linux, an executable file is a program you can run from the shell. The "path" is the filesystem location of that file, such as /usr/bin/python3 or /home/user/bin/myscript. When you type a command like ls, the shell searches through a list of directories stored in the PATH environment variable. The first match it finds is the one that runs. Knowing the exact path is useful for scripting, debugging, or understanding which version of a program you're invoking.

Using which to Find Executables

The which command is the most straightforward way to locate an executable. It searches the directories listed in your PATH and prints the full path of the first match.

  • which ls → /usr/bin/ls
  • which python3 → /usr/bin/python3
  • which -a gcc → prints all matches, not just the first

If the command isn't found, which returns nothing and exits with a non-zero status. You can check this in a script with if which mytool; then ....

Using type and command -v

These are more flexible than which because they also recognize shell built-ins, aliases, and functions.

  • type -a ls shows all locations, including aliases and functions.
  • type -p ls prints only the executable path (similar to which).
  • command -v ls works in POSIX shells and is often recommended for scripts because it's a built-in and handles aliases correctly.

For example, type cd will show cd is a shell builtin rather than a path, because cd isn't a separate executable.

What If the Executable Isn't in PATH?

If you know the file exists but which returns nothing, it's likely not in your PATH. You can search manually:

  • find / -name "myprogram" -type f 2>/dev/null — searches the whole filesystem (slow).
  • locate myprogram — uses a prebuilt database (fast, but may be outdated).
  • Check common directories like /usr/local/bin, /opt, or your home bin folder.

If you find it, you can run it with the full path, e.g., /home/user/bin/myscript, or add its directory to your PATH in ~/.bashrc.

How to Display the PATH Variable Itself

Sometimes you need to see which directories the shell searches. Use:

echo $PATH

This prints a colon-separated list like /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games. The order matters: the shell checks from left to right, so if two versions of a program exist, the first directory in PATH wins. You can temporarily add a directory with export PATH=/my/dir:$PATH (for the current session) or permanently by editing ~/.bashrc.

Common Mistakes and Pitfalls

  • Using which in scripts: It's an external command, so it's slower and may behave differently across systems. Prefer command -v in POSIX scripts.
  • Aliases: which ignores aliases, so which ls might show /usr/bin/ls even if you have an alias that points elsewhere. Use type -a ls to see the alias.
  • Case sensitivity: Linux filenames are case-sensitive. which Python3 won't find python3.
  • Symlinks: which shows the path to the symlink, not the real file. Use readlink -f $(which python3) to resolve the final target.

Alternatives: whereis and find

whereis is another option, but it searches only standard binary directories and also looks for man pages and source code. For example, whereis ls might return /usr/bin/ls /usr/share/man/man1/ls.1.gz. It's less precise for non-standard locations. For a comprehensive search, find is the most thorough but can be slow. A good middle ground is find /usr /bin /opt -name "myapp" 2>/dev/null.

Frequently Asked Questions

Why does which not find a program I just installed?

Most likely the installation directory isn't in your PATH. For example, if you installed to /opt/myapp/bin, you need to add that directory to PATH or run the executable with its full path. Also, if you installed it for your user only (e.g., in ~/.local/bin), your shell may not include that directory by default.

What's the difference between which and type?

which only searches PATH and prints executable paths. type is a shell built-in that also recognizes aliases, functions, and built-ins. So type gives a more complete picture of what the shell will actually run when you type a command.

How do I find the path of a running process?

Use readlink -f /proc/PID/exe, replacing PID with the process ID. For example, readlink -f /proc/1234/exe shows the full path of the executable that started that process. You can find the PID with pgrep or ps aux | grep name.

Can I display the path of a command without executing it?

Yes, which, type -p, and command -v all just print the path; they don't run the command. This is safe and useful for scripts that need to check if a tool exists before using it.

What does command -v do differently from which?

command -v is a shell built-in, so it's faster and more portable across different Unix-like systems. It also correctly handles aliases and functions. For example, if you have an alias for ls, command -v ls will print the alias definition, while which ls will show the binary path.