History Commands & Search
Basic History Commands
Viewing Command History
## Display entire command history
history
## Show last 10 commands
history 10
## Display with line numbers
history -w
Executing Previous Commands
## Repeat last command
!!
## Execute specific numbered command
!10
## Execute command starting with specific prefix
!git
Advanced History Search Techniques
Incremental Search
## Press Ctrl+R to start reverse search
## Type part of command to find matches
graph LR
A[Ctrl+R] --> B[Incremental Search Mode]
B --> C[Type Partial Command]
C --> D[Match Found]
D --> E[Press Enter to Execute]
Search and Filter
## Search history for specific commands
history | grep docker
## Count occurrences of commands
history | awk '{print $2}' | sort | uniq -c | sort -nr
History Command Options
Option |
Description |
Example |
-c |
Clear current session history |
history -c |
-d |
Delete specific history entry |
history -d 100 |
-w |
Write current history to file |
history -w ~/.myhistory |
Advanced Manipulation
Preventing Command Logging
## Commands starting with space are not saved
secret_command
## Disable history for current session
set +o history
LabEx Productivity Tip
LabEx recommends mastering these history search techniques to significantly improve your command-line efficiency.
Real-world Search Scenarios
## Find recent SSH connections
history | grep ssh
## Identify package installation commands
history | grep -E "apt|yum|dnf|pip"
Best Practices
- Use incremental search frequently
- Leverage line number executions
- Customize history settings
- Regularly clean unnecessary history entries