How to Find the Full Path of a Command in Linux
Learn how which, which -a, PATH, type, and command -v help you find Linux command executables and understand shell command resolution.
Why Find a Command's Full Path?
When you type a command name such as touch, the shell must determine what should run. The name you enter is not necessarily a filesystem location. It is a command name that the shell resolves to either an external executable file or a shell-provided command.
An executable is a file with permission to run as a program. An absolute path is a complete filesystem location beginning at the root directory, such as /usr/bin/touch. Finding this path can help you:
- Confirm which program will run.
- Diagnose a missing command or an unexpected program version.
- Compare multiple installed copies of a program.
- Run a specific executable directly with its absolute path.
- Check whether a command is external or provided by the shell.
How Linux Uses PATH
PATH is an environment variable containing an ordered, colon-separated list of directories. When you enter a command without a path, the shell searches these directories for a matching executable.
printf '%s\n' "$PATH"A typical value might look like this:
/usr/local/bin:/usr/bin:/binThe shell searches from left to right. If both /usr/local/bin/tool and /usr/bin/tool exist and both directories are in PATH, the copy in /usr/local/bin normally takes priority because its directory appears first.
To make the search order easier to read, print one directory per line:
printf '%s\n' "$PATH" | tr ':' '\n'Using which to Find an Executable
which is a command-line utility that locates executable files by searching directories in PATH. Its basic syntax is:
which command_nameFor example:
which touchTypical output is:
/usr/bin/touchThis is an absolute path to the first matching executable that which finds in PATH search order. The exact path can differ between Linux distributions and installations.
The command name touch is what you typed. The output /usr/bin/touch is the filesystem path to the executable that was located.
Find Every Matching Executable with which -a
Use the -a option to list every matching executable found in the directories in PATH:
which -a touchPossible output:
/usr/local/bin/touch
/usr/bin/touch
/bin/touchMultiple matches can exist when different software packages, manually installed versions, compatibility directories, or copied executables use the same command name. The results are shown in PATH search order. In the example, /usr/local/bin/touch generally has priority over the later entries.
When you need a specific copy, invoke its absolute path:
/usr/bin/touch example.txtDo this only when you intentionally want that particular executable. Otherwise, inspect and adjust PATH deliberately rather than guessing which version is active.
Interpreting Lookup Results
| Situation | Example output or behavior | Meaning | Recommended next step |
|---|---|---|---|
| External executable found | /usr/bin/touch | An executable with that name was found in a PATH directory. | Use the path to identify or directly run the program. |
| Multiple executable matches | which -a touch prints several paths. | More than one matching executable is available; the first normally has priority. | Compare the paths and inspect PATH ordering. |
| No PATH match | No output, or a not-found message. | No matching executable was found in the searched directories. | Check the name, installation, PATH, and shell command type. |
| Shell builtin | which cd may produce no useful executable path. | The command is implemented inside the shell rather than as a separate file. | Use type cd or command -v cd. |
| Alias or function | which may vary in how it reports the name. | The shell may replace the name with an alias or function. | Use the shell-aware commands type or command -v. |
Limitations: Not Every Command Is an Executable
which is primarily designed to locate external executable files. However, a shell command can also be a shell builtin, alias, function, or keyword.
- A shell builtin is implemented by the shell itself. For example,
cdmust change the current shell's directory, so it is commonly built into the shell rather than provided only by an external file. - An alias is a shell-defined shortcut that expands one command name into another command or command sequence.
- A shell function is a named block of shell commands defined in the current shell environment.
- A shell keyword is syntax recognized specially by the shell, such as
iforfor.
These command forms do not necessarily have a standalone executable path. Also, which implementations and behavior can vary somewhat across Linux distributions and shells.
Shell-Aware Alternatives: type and command -v
Use type to ask the current shell how it resolves a command:
type touch
type cdExample results might identify touch as an external file and cd as a shell builtin. The exact wording depends on the shell.
command -v is another shell-aware lookup method and is often a good choice in scripts:
command -v touchFor an external command, it commonly prints a path such as /usr/bin/touch. For a builtin or alias, it may print the command name or a description appropriate to the shell. These commands perform command resolution more closely than a standalone external lookup utility.
| Tool | Primary purpose | Searches PATH | Handles aliases, builtins, and functions | Typical use case |
|---|---|---|---|---|
which | Locate the first matching external executable. | Yes | Not reliably; behavior varies. | Quickly inspect an external command's path. |
which -a | Locate all matching external executables. | Yes | Not reliably; behavior varies. | Find duplicate installations and compare priority. |
type | Report how the current shell identifies a command. | Usually for external commands | Yes | Distinguish aliases, functions, builtins, keywords, and files. |
command -v | Perform a shell-aware command lookup. | Yes, for external commands | Generally yes | Portable command checks, especially in scripts. |
Troubleshooting Command Lookup
which Prints Nothing or Reports Not Found
Possible causes include an uninstalled program, a missing executable directory in PATH, a misspelled command name, or a command that is actually a builtin, alias, or function.
- Check how the shell resolves the name:
type command_name
command -v command_name- Inspect the current PATH and its order:
printf '%s\n' "$PATH"
printf '%s\n' "$PATH" | tr ':' '\n'- Verify that the relevant program is installed and that its executable directory is included in PATH.
which -a Shows Several Paths
This normally means multiple installations or versions are available. Read the results from top to bottom: the first result generally has priority because its directory appears earlier in PATH.
which -a command_name
printf '%s\n' "$PATH" | tr ':' '\n'Compare the listed directories with the PATH order. If necessary, run the desired version using its absolute path.
A Different Version Runs Than Expected
An earlier PATH directory may contain another executable with the same name. Use which -a to list candidates, inspect PATH ordering, and decide whether to adjust PATH for the current shell session or invoke a specific absolute path.
which Does Not Work as Expected for cd
cd is commonly a shell builtin, so there may be no standalone executable to locate. Use:
type cd
command -v cdIf the result identifies a builtin, the shell itself performs the operation. The same principle applies to aliases and functions: inspect command resolution rather than assuming every command name maps to an executable file.
Command Resolution Summary
Command resolution is the process a shell uses to determine what should run when you enter a command name. For an external command, PATH order is central: the shell searches the listed directories and normally selects the first suitable executable. For shell features such as builtins, aliases, and functions, the shell's own rules apply.
- Use
which command_nameto find the first external executable match. - Use
which -a command_nameto find all external executable matches. - Use
printf '%s\n' "$PATH" | tr ':' '\n'to inspect directory order. - Use
type command_nameto identify aliases, functions, builtins, keywords, and executables. - Use
command -v command_namefor a shell-aware lookup. - Remember that a path is returned only when a suitable executable is available through PATH.
For broader Linux command-line context, see Bourne Again Shell Bash, File Structure in Linux, and Manage File Ownership.