Which Command in Linux with Examples

Which Command shows the full path of shell commands. It is a small and simple command to locate executables in the system. It allows users to pass several command names as arguments to get their paths. The “which” command searches for the executable’s path in the system paths defined by the $PATH environment variable.

The syntax for the “which” command in Linux is:

which [options] [command]
  • options are optional arguments that can be used to modify the behavior of the command.
  • command_name is the name of the command or commands whose full path you want to find.

-a: Print all matching executables in PATH, not just the first.

Which Command Examples

Example 1: Finding the Location of a Specific Command

$ which ls

This command will find the location of the ls command, which is used for listing files and directories in Linux.


Example 2: Finding the Paths of Multiple Commands

$ which ls gdb open grep

This command will find the paths of the specified commands – ls, gdb, open, and grep commands.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Example 3: Displaying All Paths Using the -a Option

The which command has an -a option that displays all paths of executables that match the argument.

$ which echo

This will search for the “echo” executable in all paths set in the $PATH environment variable and display the first path where it is found. However, the echo executable may also be located in other paths in the $PATH environment variable. To get all paths where the echo executable is present in the system, you can use the “-a” option.

$ which -a  echo

This will display all paths where the echo executable is located in the system.

Example 4: Showing the Path as an Absolute Path

which -a -a ls

This command will ensure that it is a absolute path.


Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.