which Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
The 'which' command in Linux is a small but incredibly useful utility that helps users locate executable files within the system's PATH. It's an essential tool for system administrators, developers, and Linux enthusiasts who want to quickly identify the location of a binary file, script, or other executable they wish to use or debug.
Syntax of which Command in Linux
The basic syntax of the which command in Linux is as follows:
Where:
- [options]: The options, also known as flags, modify the behavior of the 'which' command. For example, '-a' will print all matching pathnames of each argument.
- [command_name]: This is the name of the executable file you want to locate. It is the main argument to the 'which' command.
Options in which Command in Linux
-
-a: Prints all matching pathnames of each argument.
For example -
Output:
This command displays all the paths where 'python' executable is located.
-
-i: Ignores case distinctions while matching.
For example -
Output:
This command will find 'python' even though the case doesn't match exactly.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example Usages
-
Finding the path of an executable.:
Output:
Explanation: This command prints the path of the 'ls' command.
-
Finding the path of multiple executables at once.:
Output:
Explanation: This command prints the paths of both 'ls' and 'python' commands.
Tips
-
If the 'which' command does not find the executable, it could be that the PATH environment variable is not set correctly, or the executable is not in any directory listed in PATH.
-
The 'which' command only works for executables, not for shell built-in commands.
Turn Learning into Career Growth
Advanced Use Cases of which Command in Linux
-
Using 'which' command with command substitution to check if a program is installed.:
Output:
Explanation: This command checks if 'python' is installed and prints a message with its path.
-
Finding the path of a command and navigating to its directory.:
Explanation: This command changes the current directory to the one containing the 'python' executable.
Conclusion
-
The 'which' command in Linux is a simple and effective tool for locating the paths ofexecutable files.
-
'which' operates by searching through the directories listed in the PATH environment variable.
-
It is important to remember that 'which' can only locate executables, not shell built-in commands.
-
The '-a' and '-i' options can be particularly useful for displaying all matching pathnames and ignoring case distinctions, respectively.