Linux which Command with Examples

«
»

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

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

DESCRIPTION

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 ]

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

OPTIONS

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

EXAMPLES

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.

advertisement
advertisement
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 & technical discussions at Telegram SanfoundryClasses.