Bird
Raised Fist0
Linux CLIscripting~5 mins

History command and search in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you forget the exact command you typed before or want to repeat a previous command quickly. The history command helps you see past commands, and searching lets you find the one you need without scrolling through everything.
When you want to repeat a command you ran earlier without retyping it all.
When you need to find a command you used days ago but don’t remember exactly how it looked.
When you want to check what commands you or someone else ran on the system.
When you want to save time by reusing complex commands from your history.
When you want to search for commands related to a specific task or keyword.
Commands
This command shows a numbered list of all the commands you have typed in the current shell session and previous ones saved in the history file.
Terminal
history
Expected OutputExpected
1 ls -l 2 cd /var/log 3 cat syslog 4 history
This command searches your command history for any commands containing the word 'git'. It helps you find specific commands quickly.
Terminal
history | grep git
Expected OutputExpected
15 git status 23 git commit -m 'fix bug' 42 git push origin main
Pressing Ctrl + r starts a reverse search in your command history. You can type part of a command, and it will show the most recent match. Press Enter to run it or Ctrl + r again to find earlier matches.
Terminal
Ctrl + r
Expected OutputExpected
(reverse-i-search)`git': git push origin main
This runs the command numbered 42 from your history list. It saves time by letting you rerun a command without typing it again.
Terminal
!42
Expected OutputExpected
Everything pushed to origin main branch
Key Concept

If you remember nothing else from this pattern, remember: the history command and reverse search let you quickly find and reuse past commands without retyping.

Common Mistakes
Trying to run ! without a number or valid command reference
The shell will give an error because it doesn't know which command to run.
Use ! followed by a valid history number or command prefix, like !42 or !git.
Not pressing Ctrl + r to start reverse search and instead scrolling manually
Manually scrolling through history is slow and inefficient.
Use Ctrl + r and type part of the command to quickly find it.
Using history without piping to grep when searching for a specific command
You get a long list and have to look through everything manually.
Use history | grep keyword to filter commands by keyword.
Summary
Use the history command to list all past commands with numbers.
Use history | grep keyword to search commands containing a specific word.
Use Ctrl + r to interactively search and reuse previous commands quickly.
Use !number to rerun a command from the history list by its number.

Practice

(1/5)
1. What does the history command do in a Linux terminal?
easy
A. Deletes all saved commands
B. Lists all previously executed commands
C. Shows current running processes
D. Clears the terminal screen

Solution

  1. Step 1: Understand the purpose of history

    The history command shows a list of commands you typed before in the terminal.
  2. Step 2: Compare options with command behavior

    Only Lists all previously executed commands correctly describes this behavior; others describe different commands.
  3. Final Answer:

    Lists all previously executed commands -> Option B
  4. Quick Check:

    history lists past commands [OK]
Hint: Remember: history shows past commands typed [OK]
Common Mistakes:
  • Confusing history with clearing screen
  • Thinking history deletes commands
  • Mixing history with process listing
2. Which of the following is the correct syntax to rerun the last command starting with git?
easy
A. !!git
B. !$git
C. !git
D. !^git

Solution

  1. Step 1: Recall syntax for rerunning commands by prefix

    Using !string reruns the last command starting with string.
  2. Step 2: Analyze options

    !git reruns last command starting with 'git'. Others are invalid or incorrect syntax.
  3. Final Answer:

    !git -> Option C
  4. Quick Check:

    Use !string to rerun commands [OK]
Hint: Use ! plus command start to rerun quickly [OK]
Common Mistakes:
  • Using !!git which is invalid
  • Confusing !$ with rerun syntax
  • Trying !^git which is not correct
3. Given the following history output:
  100  ls -l
101 git status
102 cd projects
103 git commit -m 'fix'

What command will run if you type !git?
medium
A. git commit -m 'fix'
B. git status
C. ls -l
D. cd projects

Solution

  1. 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'.
  2. Step 2: Understand !git runs last matching command

    !git reruns the most recent command starting with 'git', which is command 103.
  3. Final Answer:

    git commit -m 'fix' -> Option A
  4. Quick Check:

    !string runs last command starting with string [OK]
Hint: !string runs last command starting with string [OK]
Common Mistakes:
  • Choosing first matching command instead of last
  • Confusing command numbers with commands
  • Selecting unrelated commands
4. You tried to rerun a command with !cd but got an error: bash: !cd: event not found. What is the likely cause?
medium
A. The shell does not support history
B. You typed the command incorrectly
C. You need to use !! instead
D. No previous command starts with 'cd' in history

Solution

  1. Step 1: Understand error meaning

    event not found means no matching command found in history for the given prefix.
  2. Step 2: Analyze cause

    If no previous command starts with 'cd', !cd fails with this error.
  3. Final Answer:

    No previous command starts with 'cd' in history -> Option D
  4. Quick Check:

    !string fails if no matching command [OK]
Hint: Check history for matching commands before using !string [OK]
Common Mistakes:
  • Assuming syntax error instead of missing history
  • Thinking shell lacks history support
  • Using !! instead of !string incorrectly
5. You want to find and rerun a command that contains the word backup anywhere in it, but you don't remember the exact command start. Which is the best way to do this?
hard
A. Press Ctrl + r and type 'backup' to search interactively
B. Use !backup to rerun the command
C. Run history | grep backup and then rerun manually
D. Use !! backup to rerun last backup command

Solution

  1. Step 1: Understand searching commands with unknown start

    !string only matches commands starting with string, so !backup won't find commands containing 'backup' elsewhere.
  2. Step 2: Identify interactive search method

    Ctrl + r lets you search history interactively for any substring, including 'backup'.
  3. Final Answer:

    Press Ctrl + r and type 'backup' to search interactively -> Option A
  4. Quick Check:

    Use Ctrl + r for substring history search [OK]
Hint: Use Ctrl + r to search history by any word quickly [OK]
Common Mistakes:
  • Assuming !backup matches anywhere in command
  • Trying !! backup which is invalid syntax
  • Relying only on grep without rerun shortcut
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