“which” Command Usage Examples in Linux

«
»

This tutorial explains Linux “which” command, options and its usage with examples.

which – shows the full path of (shell) commands.

DESCRIPTION

advertisement

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.

SYNOPSIS

which [OPTION ][ COMMAND ]

advertisement
advertisement

OPTIONS

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

EXAMPLES

advertisement

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

advertisement

“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.

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter