History command and search in Linux CLI - Time & Space Complexity
When using the history command with search, we want to know how long it takes to find a past command as the history grows.
We ask: How does the search time change when the history list gets bigger?
Analyze the time complexity of the following code snippet.
history | grep "search_term"
This code lists all past commands and filters them to show only those containing "search_term".
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each command line in history to see if it contains the search term.
- How many times: Once for every command stored in history.
As the number of commands in history grows, the search checks more lines one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of commands in history.
Time Complexity: O(n)
This means the search time grows in a straight line as the history list gets longer.
[X] Wrong: "The search is instant no matter how big the history is."
[OK] Correct: The command checks each line one by one, so more history means more work and longer search time.
Understanding how search time grows helps you explain efficiency in real tasks, showing you know how tools behave as data grows.
"What if we used a command that indexed history for faster search? How would the time complexity change?"
Practice
history command do in a Linux terminal?Solution
Step 1: Understand the purpose of
Thehistoryhistorycommand shows a list of commands you typed before in the terminal.Step 2: Compare options with command behavior
Only Lists all previously executed commands correctly describes this behavior; others describe different commands.Final Answer:
Lists all previously executed commands -> Option BQuick Check:
historylists past commands [OK]
- Confusing history with clearing screen
- Thinking history deletes commands
- Mixing history with process listing
git?Solution
Step 1: Recall syntax for rerunning commands by prefix
Using!stringreruns the last command starting withstring.Step 2: Analyze options
!gitreruns last command starting with 'git'. Others are invalid or incorrect syntax.Final Answer:
!git -> Option CQuick Check:
Use!stringto rerun commands [OK]
- Using !!git which is invalid
- Confusing !$ with rerun syntax
- Trying !^git which is not correct
100 ls -l
101 git status
102 cd projects
103 git commit -m 'fix'
What command will run if you type
!git?Solution
Step 1: Identify last command starting with 'git'
From history, commands 101 and 103 start with 'git'. The last one is 103:git commit -m 'fix'.Step 2: Understand
!gitruns last matching command!gitreruns the most recent command starting with 'git', which is command 103.Final Answer:
git commit -m 'fix' -> Option AQuick Check:
!stringruns last command starting with string [OK]
- Choosing first matching command instead of last
- Confusing command numbers with commands
- Selecting unrelated commands
!cd but got an error: bash: !cd: event not found. What is the likely cause?Solution
Step 1: Understand error meaning
event not foundmeans no matching command found in history for the given prefix.Step 2: Analyze cause
If no previous command starts with 'cd',!cdfails with this error.Final Answer:
No previous command starts with 'cd' in history -> Option DQuick Check:
!stringfails if no matching command [OK]
- Assuming syntax error instead of missing history
- Thinking shell lacks history support
- Using !! instead of !string incorrectly
backup anywhere in it, but you don't remember the exact command start. Which is the best way to do this?Solution
Step 1: Understand searching commands with unknown start
!stringonly matches commands starting with string, so!backupwon't find commands containing 'backup' elsewhere.Step 2: Identify interactive search method
Ctrl + rlets you search history interactively for any substring, including 'backup'.Final Answer:
Press Ctrl + r and type 'backup' to search interactively -> Option AQuick Check:
Use Ctrl + r for substring history search [OK]
- Assuming !backup matches anywhere in command
- Trying !! backup which is invalid syntax
- Relying only on grep without rerun shortcut
Ready to prove it?
Pass the test and this pattern moves unknown
