How to Use which Command in Linux: Syntax and Examples
The
which command in Linux shows the full path of executables that would run when you type a command. Use which command_name to find where that command is located in your system's PATH.Syntax
The basic syntax of the which command is:
which [options] command_name
Here, command_name is the name of the program or command you want to locate. Options can modify the behavior but are optional.
bash
which command_name
Example
This example shows how to find the location of the python3 executable on your system. It helps you know which version or path will run when you type python3.
bash
which python3
Output
/usr/bin/python3
Common Pitfalls
Sometimes which returns no output if the command is not in your PATH or is a shell builtin. Also, which only shows the first match in PATH, so if you have multiple versions, it won't show all.
Wrong usage example:
which
This gives an error because you must specify a command name.
Right usage example:
which ls
bash
which which ls
Output
which: no command specified
/bin/ls
Quick Reference
| Option | Description |
|---|---|
| -a | Show all matching executables in PATH, not just the first |
| -s | Silent mode, no output, only return status |
| -h | Show help information |
Key Takeaways
Use
which command_name to find the full path of a command executable.which only shows the first match in your PATH by default.If
which returns nothing, the command may not be installed or is a shell builtin.Use
which -a to see all locations of a command in your PATH.Always specify a command name; running
which alone causes an error.