0
0
Linux CLIscripting~15 mins

which and whereis for commands in Linux CLI - Deep Dive

Choose your learning style9 modes available
Overview - which and whereis for commands
What is it?
The commands 'which' and 'whereis' help you find where programs or commands are located on your Linux system. 'which' shows the path of the executable that runs when you type a command. 'whereis' gives more information, including locations of the executable, source code, and manual pages. Both are useful to understand what runs when you enter a command.
Why it matters
Without these commands, you might not know which version of a program runs or where it is stored. This can cause confusion if multiple versions exist or if a command is missing. Knowing the exact location helps in troubleshooting, scripting, and managing your system effectively.
Where it fits
Before learning these, you should understand basic Linux commands and the concept of the PATH environment variable. After mastering these, you can explore more advanced command discovery tools like 'type' and 'command -v', and learn about shell scripting and environment management.
Mental Model
Core Idea
'which' and 'whereis' are tools that tell you where commands live on your system, helping you see exactly what runs when you type a command.
Think of it like...
It's like asking 'which key opens this door?' and 'where are all the keys and instructions for this door?' 'which' finds the key you use, 'whereis' shows all related keys and manuals.
┌─────────────┐       ┌───────────────┐
│ User types  │──────▶│ Shell looks   │
│ command     │       │ in PATH        │
└─────────────┘       └───────────────┘
         │                     │
         │                     ▼
         │             ┌───────────────┐
         │             │ 'which' shows │
         │             │ executable    │
         │             │ path          │
         │             └───────────────┘
         │
         ▼
┌─────────────────┐
│ 'whereis' shows │
│ executable,     │
│ source, manual  │
│ locations       │
└─────────────────┘
Build-Up - 6 Steps
1
FoundationUnderstanding command execution path
🤔
Concept: Commands run by typing their name are found by the shell searching directories listed in the PATH variable.
When you type a command like 'ls', the shell looks through directories in the PATH environment variable one by one to find an executable file named 'ls'. The first match it finds is the program it runs.
Result
The shell runs the first executable it finds in PATH matching the command name.
Understanding PATH is key to knowing why commands run the way they do and why multiple versions can cause confusion.
2
FoundationBasic use of 'which' command
🤔
Concept: 'which' shows the full path of the executable that will run for a command.
Run 'which ls' and it will output something like '/bin/ls'. This tells you exactly which 'ls' program runs when you type 'ls'.
Result
/bin/ls
'which' helps you confirm the exact program your shell will execute, avoiding guesswork.
3
IntermediateExploring 'whereis' command details
🤔Before reading on: do you think 'whereis' only shows the executable location or more? Commit to your answer.
Concept: 'whereis' locates the binary, source code, and manual pages for a command.
Run 'whereis ls' and you might see: 'ls: /bin/ls /usr/share/man/man1/ls.1.gz'. This shows the executable and the manual page location.
Result
ls: /bin/ls /usr/share/man/man1/ls.1.gz
Knowing 'whereis' shows more than just the executable helps you find documentation and source files quickly.
4
IntermediateDifferences between 'which' and 'whereis'
🤔Before reading on: do you think 'which' and 'whereis' always show the same paths? Commit to your answer.
Concept: 'which' searches the PATH for executables; 'whereis' searches standard system locations for executables, sources, and manuals.
'which' depends on your PATH variable and shows the executable that runs. 'whereis' ignores PATH and looks in fixed directories for related files.
Result
'which' output varies with PATH; 'whereis' output is broader and fixed.
Understanding their search methods clarifies why outputs differ and when to use each.
5
AdvancedUsing 'which' with aliases and functions
🤔Before reading on: does 'which' show aliases or shell functions? Commit to your answer.
Concept: 'which' only shows executables, not shell aliases or functions.
If you have an alias 'll' for 'ls -l', 'which ll' may show nothing or just the alias name, depending on shell. Use 'type ll' to see aliases or functions.
Result
'which ll' may output nothing or 'll: aliased to ls -l' in some shells.
Knowing 'which' limitations prevents confusion when commands are aliases or functions.
6
ExpertInternal working of 'which' and 'whereis'
🤔Before reading on: do you think 'which' and 'whereis' use the same search method internally? Commit to your answer.
Concept: 'which' searches directories in PATH sequentially; 'whereis' searches predefined system directories and databases.
'which' reads PATH and checks each directory for the executable file. 'whereis' uses a database or searches standard locations like /bin, /usr/bin, /usr/local/bin, and man directories.
Result
'which' output depends on PATH order; 'whereis' output depends on system-wide locations.
Understanding their internal search strategies explains why they behave differently and how system configuration affects results.
Under the Hood
'which' reads the PATH environment variable, splits it into directories, and checks each directory in order for an executable file matching the command name. It stops at the first match and returns its full path. 'whereis' either queries a prebuilt database or scans standard system directories for binaries, source files, and manual pages related to the command name. It does not rely on PATH and returns all found locations.
Why designed this way?
'which' was designed to quickly find the executable that will run in the current shell environment, respecting user PATH settings. 'whereis' was designed to provide a broader overview of command-related files, useful for locating documentation and source code, independent of user environment. This separation allows users to choose the tool based on their need for either environment-specific or system-wide information.
PATH variable: /usr/local/bin:/usr/bin:/bin

User types command
       │
       ▼
+-----------------+       +-------------------+
| 'which' command  |──────▶| Checks each PATH   |
|                 |       | directory in order |
+-----------------+       +-------------------+
       │                         │
       ▼                         ▼
Returns first executable     +-------------------+
found in PATH directories    | 'whereis' command  |
                             |                   |
                             +-------------------+
                                      │
                                      ▼
                      Searches fixed system dirs:
                      /bin, /usr/bin, /usr/local/bin,
                      /usr/share/man, /usr/src
                                      │
                                      ▼
                      Returns all found related files
Myth Busters - 4 Common Misconceptions
Quick: Does 'which' show aliases or shell functions? Commit to yes or no before reading on.
Common Belief:'which' shows the location of any command, including aliases and shell functions.
Tap to reveal reality
Reality:'which' only shows the path of executable files, not aliases or shell functions.
Why it matters:Relying on 'which' to find aliases can cause confusion when commands behave differently than expected.
Quick: Does 'whereis' depend on the PATH environment variable? Commit to yes or no before reading on.
Common Belief:'whereis' searches the same directories as 'which' by using the PATH variable.
Tap to reveal reality
Reality:'whereis' ignores PATH and searches fixed system directories and databases instead.
Why it matters:Expecting 'whereis' to reflect user environment can lead to missing or unexpected results.
Quick: Will 'which' always find a command if it exists somewhere on the system? Commit to yes or no before reading on.
Common Belief:'which' will find any command installed on the system regardless of PATH settings.
Tap to reveal reality
Reality:'which' only finds commands in directories listed in the PATH variable.
Why it matters:Commands outside PATH won't be found by 'which', causing false assumptions about availability.
Quick: Does 'whereis' always find the source code for a command? Commit to yes or no before reading on.
Common Belief:'whereis' always locates the source code of commands if it exists.
Tap to reveal reality
Reality:'whereis' only finds source code if it is in standard locations; many programs do not have source installed or in those places.
Why it matters:Assuming source code is always found can mislead developers trying to inspect or modify programs.
Expert Zone
1
'which' behavior can vary between shells; some shells have built-in 'which' that also detect aliases and functions, while the external 'which' command does not.
2
'whereis' output depends on system configuration and installed documentation; minimal installations may show fewer results.
3
The PATH order affects which executable 'which' finds first, so changing PATH can change command behavior without changing files.
When NOT to use
'which' and 'whereis' are not reliable for detecting shell built-ins, aliases, or functions; use 'type' or 'command -v' instead. For scripts needing precise command resolution, prefer 'command -v' as it is POSIX compliant and more consistent.
Production Patterns
System administrators use 'which' to verify command locations before scripting to avoid unexpected behavior. Developers use 'whereis' to locate documentation and source files quickly. In complex environments, combining 'which', 'whereis', and 'type' helps diagnose command conflicts and environment issues.
Connections
PATH environment variable
'which' directly depends on PATH to find executables.
Understanding PATH is essential to grasp why 'which' shows certain results and how command lookup works.
Shell built-ins and aliases
'which' and 'whereis' do not detect shell built-ins or aliases; 'type' command does.
Knowing the limits of 'which' and 'whereis' helps you choose the right tool to understand command behavior fully.
Library cataloging systems
'whereis' is like a library catalog that indexes books (executables, sources, manuals) in fixed locations.
This connection shows how indexing and searching fixed locations can speed up finding related resources.
Common Pitfalls
#1Assuming 'which' shows aliases or functions.
Wrong approach:which ll
Correct approach:type ll
Root cause:Misunderstanding that 'which' only finds executables, not shell aliases or functions.
#2Expecting 'whereis' to respect user PATH settings.
Wrong approach:whereis mycustomcommand
Correct approach:which mycustomcommand
Root cause:Confusing 'whereis' system-wide search with PATH-based search.
#3Using 'which' to find commands not in PATH.
Wrong approach:which /opt/custom/bin/mycmd
Correct approach:ls /opt/custom/bin/mycmd
Root cause:Not realizing 'which' only searches PATH directories, not arbitrary paths.
Key Takeaways
'which' shows the path of the executable that runs for a command based on your PATH environment variable.
'whereis' searches fixed system directories to find executables, source code, and manual pages related to a command.
'which' does not detect shell aliases or functions; use 'type' or 'command -v' for that purpose.
Understanding the difference between 'which' and 'whereis' helps you troubleshoot command conflicts and find documentation efficiently.
Knowing how these commands work internally clarifies why their outputs differ and how your environment affects command execution.