Command History¶
Every command you execute in the Linux shell is recorded in your command history. Learning how to view, search, reuse, and manage command history can dramatically improve your productivity and help you work faster on Linux systems.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand how Bash stores command history
- View and search previous commands
- Re-execute commands from history
- Customize history behavior
- Clear and manage history safely
- Improve productivity using history shortcuts
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 Lessons 1–7
Why Learn Command History?¶
Imagine you previously executed a complex command like:
Instead of typing it again, Linux allows you to retrieve it instantly.
Command history helps you:
- Save time
- Reduce typing errors
- Repeat complex commands
- Troubleshoot efficiently
- Learn from previously executed commands
What is Command History?¶
Bash automatically records the commands you execute during a session.
Example:
Each command is assigned a unique history number.
Display Command History¶
Example output:
Display Last Few Commands¶
Display the last five commands:
Example:
Execute Previous Commands¶
Run a command by its history number.
Example:
Executes command number 98.
Execute the Last Command¶
Run the previous command again.
Example:
Scenario:
Permission denied.
Instead of typing again:
Bash automatically executes:
This is one of the most useful Bash shortcuts.
Execute Commands by Prefix¶
Run the most recent command starting with:
If your previous command was:
It runs automatically.
Other examples:
Search Command History¶
One of Bash's most powerful features.
Press:
Example:
Search:
Output:
Press:
- Ctrl + R again for older matches
- Enter to execute
- Esc to edit before execution
Search Using grep¶
Display commands containing a keyword.
Example:
Another example:
History File¶
Bash stores history in:
View it:
History Variables¶
Display history size.
Example:
Display history file size.
Example:
View history file location.
Output:
Customize History Size¶
Temporary change:
Persistent change:
Edit:
Add:
Apply changes:
Ignore Duplicate Commands¶
Prevent consecutive duplicate commands.
Ignore duplicates and commands beginning with a space.
Ignore Specific Commands¶
Example:
These commands won't be saved.
Clear Command History¶
Clear the current session.
Clear and save.
Delete a Specific Entry¶
Delete history entry number 45.
Append Current Session¶
Write current session to history file.
Read history file.
Rewrite history file.
Useful Keyboard Shortcuts¶
| Shortcut | Purpose |
|---|---|
| ↑ | Previous command |
| ↓ | Next command |
| Ctrl + R | Reverse search |
| Ctrl + P | Previous command |
| Ctrl + N | Next command |
| !! | Previous command |
| !number | Execute command by number |
| !string | Execute last command starting with string |
Command Summary¶
| Command | Purpose |
|---|---|
| history | Display history |
| history 10 | Last 10 commands |
| !! | Previous command |
| !25 | Execute command 25 |
| !git | Execute last git command |
| history -c | Clear history |
| history -d | Delete entry |
| history | grep | Search history |
Production Perspective¶
Command history is extremely useful for:
- Recovering previously used commands
- Debugging production issues
- Repeating deployment commands
- Auditing troubleshooting steps
- Learning from experienced administrators
Example:
During an incident, engineers often search history to reuse working commands quickly.
Hands-on Lab¶
Task 1¶
View command history.
Task 2¶
Display the last 10 commands.
Task 3¶
Search for ls.
Task 4¶
Run the previous command.
Task 5¶
Display history file.
Task 6¶
Display history variables.
Task 7¶
Try reverse search.
Search for:
Command Deep Dive¶
| Command | Purpose | Common Options | Production Example |
|---|---|---|---|
| history | Display command history | -c, -d, -a, -w | Review previous troubleshooting steps |
| !! | Execute previous command | Default | Retry failed command with sudo |
| !number | Execute by history number | Default | Repeat deployment command |
| Ctrl + R | Search history | Interactive | Find previous kubectl commands |
Production Troubleshooting Scenario¶
Scenario
An application stopped responding after a deployment.
You need to determine what commands were executed earlier.
Tasks:
- Display recent history.
- Search for
kubectlcommands. - Search for
dockercommands. - Re-run the last
systemctlcommand. - Save the current session history.
Example:
Mini Challenge¶
Perform the following:
- Execute at least 10 Linux commands.
- Display your command history.
- Search for
ls. - Search for
cd. - Execute the last
pwdcommand using history. - Display your history file.
- Delete one history entry.
- Clear the current session history.
Best Practices¶
- Use Ctrl + R instead of retyping long commands.
- Increase
HISTSIZEfor larger command history. - Ignore duplicate commands to keep history clean.
- Avoid storing sensitive commands containing passwords.
- Review history during troubleshooting to retrace your steps.
Common Mistakes¶
❌ Re-typing long commands repeatedly.
✅ Use:
❌ Clearing history without understanding the consequences.
✅ Use:
This removes the current session history.
❌ Assuming history is shared across all users.
✅ Each user has their own history file.
Interview Questions¶
Beginner¶
- What command displays Bash history?
- What does
!!do? - How do you search command history?
- Where is Bash history stored?
Intermediate¶
- Explain
HISTSIZEandHISTFILESIZE. - How do you ignore duplicate commands?
- What is the purpose of
history -aandhistory -w? - How do you execute a command by its history number?
Architect Level¶
- How can command history improve incident response?
- What are the security considerations when storing command history?
- How would you configure history for administrators on production servers?
Summary¶
In this lesson, you learned:
- Viewing command history
- Searching history
- Re-executing previous commands
- Managing history files
- Customizing history behavior
- Productivity shortcuts
Command history is one of Bash's most valuable features. Mastering it will help you work faster, avoid repetitive typing, and troubleshoot systems more efficiently.
Key Takeaways¶
historydisplays previously executed commands.Ctrl + Ris the fastest way to search command history.!!repeats the last command.!numberexecutes a command by its history number.HISTSIZEandHISTFILESIZEcontrol history storage.- Command history can significantly improve productivity and troubleshooting.
What's Next?¶
Input, Output, Pipes, and Redirection
In the next lesson, you'll learn:
- Standard Input (
stdin) - Standard Output (
stdout) - Standard Error (
stderr) - Output redirection (
>,>>) - Input redirection (
<) - Pipes (
|) - The
teecommand - Building powerful Linux command pipelines