This tutorial explains Linux “which” command, options and its usage with examples.
Which command is very small and simple command to locate executables in the system. It allows user to pass several command names as arguments to get their paths in the system. “which” commands searches the path of executable in system paths set in $PATH environment variable.
which [OPTION ][ COMMAND ]
Print all matching executables in PATH, not just the first.
1. Show path of certain commands
$ which ls gdb open grep /bin/ls /usr/bin/gdb /bin/open /bin/grep
It locates command names – “ls”, “gdb”, “open” and “grep” specified as arguments to “which” command and displays paths of each executable where it exists in the system.
2. Display all the paths using -a option
“which” command gives option “-a” that displays all paths of executable matching to argument.
$ which echo /usr/sbin/echo
Above will search display the executable “echo” from all paths set in $PATH environment variable and displays the first path where echo executable is found. It may be case that executable is placed at other paths of $PATH environment variable as well. To get all paths where executable is present in the system, “-a” option can be used.
$ which -a echo /usr/sbin/echo /bin/echo
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.