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.
Syntax:
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.
Options:
-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.
Output:
/usr/bin/ls
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.
Output:
/bin/ls /usr/bin/gdb /bin/open /bin/grep
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 /usr/sbin/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 /usr/sbin/echo /bin/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.
- Check Linux Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books