which and whereis for commands in Linux CLI - Time & Space Complexity
We want to understand how the time it takes to find command locations grows as the system has more commands or files.
How does searching for commands with which and whereis scale when the system gets bigger?
Analyze the time complexity of these commands.
which ls
whereis ls
which searches the directories in the PATH to find the executable location. whereis searches standard binary, source, and manual directories for the command.
Both commands repeat searches through directories.
- Primary operation: Scanning directories and checking files for the command name.
- How many times: Once per directory in PATH for
which, and multiple standard directories forwhereis.
As the number of directories to search grows, the time to find the command grows roughly in proportion.
| Input Size (directories to search) | Approx. Operations |
|---|---|
| 10 | 10 directory checks |
| 100 | 100 directory checks |
| 1000 | 1000 directory checks |
Pattern observation: The time grows linearly as more directories are checked.
Time Complexity: O(n)
This means the time to find a command grows directly with the number of directories searched.
[X] Wrong: "which and whereis instantly find commands no matter how many directories there are."
[OK] Correct: Both commands must check directories one by one, so more directories mean more work and longer search time.
Understanding how simple searches scale helps you reason about command lookups and file system operations in real systems.
What if which searched directories in parallel? How would the time complexity change?
Practice
which command do in Linux?Solution
Step 1: Understand the purpose of
Thewhichwhichcommand searches your PATH environment to find the executable file that runs when you enter a command.Step 2: Compare with other options
whereislists multiple locations including source and man files, not just the executable path. Manual pages are shown byman. Directory contents are shown byls.Final Answer:
Shows the full path of the executable that runs when you type a command -> Option CQuick Check:
which= executable path [OK]
- Confusing which with whereis
- Thinking which shows manual pages
- Assuming which lists all related files
ls command using which?Solution
Step 1: Recall basic
The basic syntax to find the path of a command is simplywhichsyntaxwhich command_name. No extra flags are needed for a single path.Step 2: Analyze options
Options like-lor--allare not standard or needed for basic usage. Adding-aor extra flags incorrectly after the command name is invalid.Final Answer:
which ls -> Option BQuick Check:
Simple syntax: which + command [OK]
- Adding unnecessary flags to which
- Placing flags after command name
- Using incorrect or unsupported options
whereis bash on a typical Linux system?Solution
Step 1: Understand
whereisoutput formatwhereisshows multiple locations: the binary executable path, source files if any, and manual page locations.Step 2: Analyze typical output for
On most systems,bashwhereis bashreturns the binary path like/bin/bashand the man page location like/usr/share/man/man1/bash.1.gz.Final Answer:
bash: /bin/bash /usr/share/man/man1/bash.1.gz -> Option AQuick Check:
whereis= binary + man files [OK]
- Expecting only one path from whereis
- Confusing whereis output with which output
- Thinking whereis returns error if command exists
which python3 but get no output. What is the most likely reason?Solution
Step 1: Interpret empty output from
Ifwhichwhichreturns nothing, it means it did not find the executable in any directory listed in your PATH environment variable.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.Final Answer:
Python3 is not installed or not in your PATH -> Option DQuick Check:
No output from which = command missing or PATH issue [OK]
- Assuming which is broken
- Blaming firewall for missing executable
- Ignoring PATH environment variable
gcc compiler including binary, source, and manual files. Which command should you use?Solution
Step 1: Understand the purpose of each command
whichshows only the executable path.locatesearches a database for filenames but may show unrelated files.findsearches the entire filesystem but is slow and may produce too many results.Step 2: Identify the command that shows binary, source, and manual files
whereisis designed to show all related locations for a command including binary, source, and man pages, making it the best choice.Final Answer:
whereis gcc -> Option AQuick Check:
whereis = binary + source + man files [OK]
- Using which to find all files
- Using locate without filtering results
- Running find without knowing its slowness
Ready to prove it?
Pass the test and this pattern moves unknown
