The history command in Linux lets you view, search, and reuse previously executed commands. It’s a huge time-saver for developers, sysadmins, and anyone working in the terminal.
🔎 What is history?
history displays a list of commands you’ve run in your shell session.
👉 Each command is stored with a number
👉 You can quickly rerun or search past commands
✅ Basic Usage
history
👉 Shows a numbered list of recent commands
🔁 Run a Previous Command
!100
👉 Runs command number 100 from history
🔄 Run the Last Command Again
!!
👉 Repeats the most recent command
🔍 Search Command History
Search with grep
history | grep ssh
👉 Finds all commands containing ssh
Reverse Search (Best Method)
Press:
Ctrl + RThen type a keyword 👉 it searches interactively
🧹 Clear History
history -c
👉 Clears current session history
💾 Save History Manually
history -w
👉 Writes history to file
📂 Where History is Stored
Most shells store history in:
~/.bash_history
⚙️ Useful Options
| Command | Description |
|---|---|
history 10 | Show last 10 commands |
history -d 100 | Delete command #100 |
history -c | Clear history |
history -a | Append new commands to file |
🚀 Real-World Examples
Re-run last sudo command
sudo !!
Find a previous install command
history | grep apt
Quickly repeat a long command
!ssh
👉 Runs last command starting with ssh
⚠️ Important Notes
- History may not save immediately unless configured
- Sensitive commands (passwords) may be stored
- Clearing history doesn’t always erase system logs
🧠 Pro Tips
- Use Ctrl + R instead of scrolling
Increase history size:
export HISTSIZE=5000export HISTFILESIZE=10000Avoid saving sensitive commands:
export HISTCONTROL=ignoreboth
👍 Quick Summary
historyshows past commands- Use
!numberto rerun commands - Use
Ctrl + Rto search instantly - Stored in
~/.bash_history
🎯 Final Thoughts
The history command is one of the fastest ways to work smarter in Linux. Once you start using reverse search and command recall, you’ll rarely need to type long commands again.