A
cd ..
Tools

History & Command Recall

Navigate, search, and reuse command history efficiently.

2025-11-29
history, bash, shell

View history

history
history 20  # Last 20 commands

Execute from history

!number     # Execute command number
!-2         # Execute 2 commands ago
!!          # Repeat last command
sudo !!     # Run last command with sudo

Search history

Ctrl+R      # Reverse search
Ctrl+R again # Next match
Ctrl+G      # Cancel search

Search by string

!string     # Last command starting with string
!?string    # Last command containing string

Execute and print

!number:p   # Print command#number without executing
!!:p        # Print last command

Clear history

history -c  # Clear session history
> ~/.bash_history  # Clear history file

Delete specific entry

history -d number

History size

# In ~/.bashrc
HISTSIZE=10000        # Commands in memory
HISTFILESIZE=20000    # Commands in file

Ignore duplicates

# In ~/.bashrc
HISTCONTROL=ignoredups          # Ignore consecutive duplicates
HISTCONTROL=ignorespace         # Ignore commands starting with space
HISTCONTROL=ignoreboth          # Both of the above  

Ignore specific commands

# In ~/.bashrc
HISTIGNORE="ls:cd:pwd:exit:date"

Timestamp

# In ~/.bashrc
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  "

# View
history

Preserve history

# In ~/.bashrc
shopt -s histappend  # Append to history file
PROMPT_COMMAND="history -a"  # Save after each command

Word designators

!!:0        # Command
 !!:1        # First argument
!!:$        # Last argument
!!:*        # All arguments
!!:2-4      # Arguments 2-4

Quick substitution

^old^new    # Replace first occurrence
!!:gs/old/new  # Replace all occurrences

Previous command arguments

!$          # Last argument of previous command
!^          # First argument of previous command
!*          # All arguments of previous command
Alt+.       # Insert last argument (repeat for older)

Search history file

grep "pattern" ~/.bash_history
cat ~/.bash_history | grep ssh

Save current session

history -a  # Append current session to history file
history -w  # Write entire history to file

Reload history

history -r  # Read history file into current session

Multiple sessions

# In ~/.bashrc - sync all sessions
shopt -s histappend
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Export history

history > my_commands.txt

Analyze history

# Most used commands
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

# Commands by hour
history | grep "$(date +%Y-%m-%d)" | cut -c 8-9 | sort | uniq -c

FC command (edit and execute)

fc          # Edit last command in $EDITOR
fc 100 105  # Edit commands 100-105
fc -l       # List recent commands
fc -s old=new 100  # Substitute and execute

Keyboard shortcuts

Ctrl+R      Reverse search
Ctrl+S      Forward search  
Ctrl+P      Previous command (↑)
Ctrl+N      Next command (↓)
Ctrl+A      Beginning of line
Ctrl+E      End of line
Ctrl+U      Clear line before cursor
Ctrl+K      Clear line after cursor
Alt+.       Last argument
Alt+B       Back one word
Alt+F       Forward one word

History expansion

!!          # Last command
!-n         # n commands ago
!string     # Last command starting with string
!?string?   # Last command containing string
^str1^str2  # Quick substitution
!#          # Entire current command line

Practical examples

Repeat with modification

# Original
git commit -m "message"

# Repeat withchange
^commit^push
# Executes: git push -m "message"

Use previous arguments

cat /very/long/path/file.txt
vim !$
# Opens: vim /very/long/path/file.txt

Fix recent command

systemctl status nginx
# Oops, need sudo
sudo !!

Build on previous

cd /var/log
ls !$  # ls /var/log

Best practices

# ~/.bashrc
HISTSIZE=50000
HISTFILESIZE=100000
HISTCONTROL=ignoreboth
HISTIGNORE="ls:ll:cd:pwd:clear:history"
HISTTIMEFORMAT="%F %T "
shopt -s histappend
PROMPT_COMMAND="history -a"

Troubleshooting

History not saving

# Check permissions
ls -la ~/.bash_history

# Fix
chmod 600 ~/.bash_history

# Check HISTFILE
echo $HISTFILE

Lost history

# Check if file exists
cat ~/.bash_history

# Restore from backup
cp ~/.bash_history.bak ~/.bash_history

Was this useful?

Share with your team

Browse More