Bird
Raised Fist0
Linux CLIscripting~10 mins

which and whereis for commands in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - which and whereis for commands
User types command
↓
which searches PATH
↓
Returns first executable path or nothing
↓
whereis searches binaries, sources, man pages
↓
Returns all found locations or nothing
The flow shows how 'which' finds the first executable in PATH, while 'whereis' searches for binaries, sources, and manuals.
Execution Sample
Linux CLI
which ls
whereis ls
Shows the path of the 'ls' command using 'which' and all locations using 'whereis'.
Execution Table
StepCommandActionResultOutput
1which lsSearch PATH directories in orderFind first 'ls' executable/bin/ls
2whereis lsSearch standard binary, source, man pathsFind all 'ls' related filesls: /bin/ls /usr/share/man/man1/ls.1.gz
💡 Commands finish after showing found paths or nothing if not found.
Variable Tracker
VariableStartAfter which lsAfter whereis ls
PATH/usr/local/bin:/usr/bin:/bin/usr/local/bin:/usr/bin:/bin/usr/local/bin:/usr/bin:/bin
which_resultnone/bin/ls/bin/ls
whereis_resultnonenone/bin/ls /usr/share/man/man1/ls.1.gz
Key Moments - 3 Insights
Why does 'which' only show one path but 'whereis' shows many?
'which' stops at the first executable found in PATH (see execution_table step 1). 'whereis' searches multiple standard locations and lists all matches (see step 2).
Can 'which' find commands not in PATH?
No, 'which' only searches directories listed in PATH (see variable_tracker PATH values). If a command is outside PATH, 'which' won't find it.
Does 'whereis' only find executables?
'whereis' finds executables, source files, and man pages related to the command (see execution_table step 2 output).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what output does 'which ls' produce at step 1?
A/bin/ls
B/usr/bin/ls
Cls not found
D/usr/share/man/man1/ls.1.gz
💡 Hint
Check the 'Output' column in execution_table row for step 1.
At which step does 'whereis ls' show man page location?
AStep 1
BNo step shows man page
CStep 2
DBoth steps
💡 Hint
Look at the 'Output' column in execution_table for step 2.
If PATH did not include /bin, what would 'which ls' output be?
A/bin/ls
BNo output (not found)
C/usr/share/man/man1/ls.1.gz
D/usr/bin/ls
💡 Hint
Refer to variable_tracker PATH and which_result values.
Concept Snapshot
which command: searches PATH directories in order and returns the first executable found.
whereis command: searches standard locations for binaries, source files, and man pages, returning all matches.
Use which to find exact executable location.
Use whereis to find related files and documentation.
If command not in PATH, which won't find it but whereis might.
Both commands help locate commands but differ in scope.
Full Transcript
This lesson shows how the Linux commands 'which' and 'whereis' find command locations. When you type 'which ls', it looks through the directories listed in your PATH environment variable, one by one, until it finds the first executable named 'ls'. It then shows that path, for example '/bin/ls'. On the other hand, 'whereis ls' searches more places, including standard binary folders, source code locations, and manual pages. It lists all found files related to 'ls', such as '/bin/ls' and '/usr/share/man/man1/ls.1.gz'. The execution table traces these steps clearly. Variables like PATH and the results of each command are tracked to show changes. Key moments clarify why 'which' shows only one path while 'whereis' shows many, and why 'which' depends strictly on PATH. The quiz tests understanding by asking about outputs at specific steps and effects of changing PATH. Remember, use 'which' to find the exact executable your shell will run, and 'whereis' to find related files and documentation.

Practice

(1/5)
1. What does the which command do in Linux?
easy
A. Lists all files related to a command including source and man pages
B. Displays the manual pages of a command
C. Shows the full path of the executable that runs when you type a command
D. Shows the current directory contents

Solution

  1. Step 1: Understand the purpose of which

    The which command searches your PATH environment to find the executable file that runs when you enter a command.
  2. Step 2: Compare with other options

    whereis lists multiple locations including source and man files, not just the executable path. Manual pages are shown by man. Directory contents are shown by ls.
  3. Final Answer:

    Shows the full path of the executable that runs when you type a command -> Option C
  4. Quick Check:

    which = executable path [OK]
Hint: Remember: which = executable path only [OK]
Common Mistakes:
  • Confusing which with whereis
  • Thinking which shows manual pages
  • Assuming which lists all related files
2. Which of the following is the correct syntax to find the location of the ls command using which?
easy
A. which -l ls
B. which ls
C. which --all ls
D. which ls -a

Solution

  1. Step 1: Recall basic which syntax

    The basic syntax to find the path of a command is simply which command_name. No extra flags are needed for a single path.
  2. Step 2: Analyze options

    Options like -l or --all are not standard or needed for basic usage. Adding -a or extra flags incorrectly after the command name is invalid.
  3. Final Answer:

    which ls -> Option B
  4. Quick Check:

    Simple syntax: which + command [OK]
Hint: Use just 'which command' to find executable path [OK]
Common Mistakes:
  • Adding unnecessary flags to which
  • Placing flags after command name
  • Using incorrect or unsupported options
3. What will be the output of the command whereis bash on a typical Linux system?
medium
A. bash: /bin/bash /usr/share/man/man1/bash.1.gz
B. bash: command not found
C. /usr/bin/bash
D. /bin/bash

Solution

  1. Step 1: Understand whereis output format

    whereis shows multiple locations: the binary executable path, source files if any, and manual page locations.
  2. Step 2: Analyze typical output for bash

    On most systems, whereis bash returns the binary path like /bin/bash and the man page location like /usr/share/man/man1/bash.1.gz.
  3. Final Answer:

    bash: /bin/bash /usr/share/man/man1/bash.1.gz -> Option A
  4. Quick Check:

    whereis = binary + man files [OK]
Hint: whereis shows binary and man page locations [OK]
Common Mistakes:
  • Expecting only one path from whereis
  • Confusing whereis output with which output
  • Thinking whereis returns error if command exists
4. You run which python3 but get no output. What is the most likely reason?
medium
A. Python3 is installed but hidden by a firewall
B. You typed the command incorrectly
C. which command is broken on your system
D. Python3 is not installed or not in your PATH

Solution

  1. Step 1: Interpret empty output from which

    If which returns nothing, it means it did not find the executable in any directory listed in your PATH environment variable.
  2. Step 2: Consider common causes

    Most often, this means the program is not installed or its executable directory is not in PATH. Typing errors usually cause an error message, not empty output. The command itself rarely breaks, and firewalls do not hide executables.
  3. Final Answer:

    Python3 is not installed or not in your PATH -> Option D
  4. Quick Check:

    No output from which = command missing or PATH issue [OK]
Hint: No output means command not found in PATH [OK]
Common Mistakes:
  • Assuming which is broken
  • Blaming firewall for missing executable
  • Ignoring PATH environment variable
5. You want to find all locations related to the gcc compiler including binary, source, and manual files. Which command should you use?
hard
A. whereis gcc
B. which gcc
C. locate gcc
D. find / -name gcc

Solution

  1. Step 1: Understand the purpose of each command

    which shows only the executable path. locate searches a database for filenames but may show unrelated files. find searches the entire filesystem but is slow and may produce too many results.
  2. Step 2: Identify the command that shows binary, source, and manual files

    whereis is designed to show all related locations for a command including binary, source, and man pages, making it the best choice.
  3. Final Answer:

    whereis gcc -> Option A
  4. Quick Check:

    whereis = binary + source + man files [OK]
Hint: Use whereis to find all related command files [OK]
Common Mistakes:
  • Using which to find all files
  • Using locate without filtering results
  • Running find without knowing its slowness
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard