Linux — Command History and Shortcuts
Linux keeps a record of every command you type. Knowing how to search and reuse this history — combined with keyboard shortcuts — dramatically speeds up your work.
history — View Past Commands
# Show all recent commands with line numbers
history
# 1 ls -la
# 2 cd /var/log
# 3 tail -f syslog
# 4 cd ~
# 5 sudo apt update
# Show last N commands
history 20
# Show history with timestamps (if HISTTIMEFORMAT is set)
HISTTIMEFORMAT="%Y-%m-%d %H:%M "
history
# 1 2025-04-01 09:00 ls -la
# 2 2025-04-01 09:05 cd /var/log
# Make timestamps permanent
echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M "' >> ~/.bashrc
# Search history with grep
history | grep "docker"
# 23 docker ps -a
# 47 docker build -t myapp .
# 89 docker-compose up -d
# Clear history
history -c # Clear in-memory history
history -w # Write (current) history to file
> ~/.bash_history # Wipe the history file (careful!)
History Expansion: Reusing Commands
!! — Last Command
# Repeat the last command exactly
ls -la /etc/nginx
!!
# ls -la /etc/nginx (executed again)
# Most common use: forgot sudo
apt update
# E: Could not open lock file /var/lib/dpkg/lock-frontend (Permission denied)
sudo !!
# sudo apt update ← automatically expanded
# [sudo] password for ubuntu: ••••
!n — Command by Number
# Re-run command number 47 from history
!47
# docker build -t myapp . (whatever command 47 was)
# Verify what command 47 is before running
history | grep "^ 47"
# 47 docker build -t myapp .
!string — Last Command Starting With
# Re-run the most recent command starting with "docker"
!docker
# docker-compose up -d ← last docker command
# Re-run the most recent command starting with "ssh"
!ssh
# ssh ubuntu@54.123.45.67
# Expand without executing (check first)
!docker:p
# docker-compose up -d ← printed, NOT run
!$ — Last Argument of Previous Command
# mkdir a directory and immediately cd into it
mkdir -p /tmp/newproject/src
cd !$
# cd /tmp/newproject/src
# Create a file then open it in vim
touch /etc/nginx/conf.d/myapp.conf
vim !$
# vim /etc/nginx/conf.d/myapp.conf
Ctrl+R — Reverse Incremental Search
This is the most powerful history feature — search through history as you type:
# Press Ctrl+R and start typing
(reverse-i-search)`':
(reverse-i-search)`do': docker-compose up -d
# Ctrl+R again → go to the previous match
# Enter → run the found command
# → (right arrow) → edit the command before running
# Ctrl+G or Ctrl+C → cancel the search
# Example workflow:
# You want to re-run a long docker command from earlier
1. Press Ctrl+R
(reverse-i-search)`:
2. Type "docker build"
(reverse-i-search)`docker build': docker build -t myapp:latest --build-arg ENV=prod .
3. Press Enter to run it
OR press → to copy it to the command line for editing
Readline Keyboard Shortcuts
These shortcuts work in bash (and most Linux terminals):
Cursor Movement
| Shortcut | Action |
|---|---|
Ctrl+A |
Jump to beginning of line |
Ctrl+E |
Jump to end of line |
Alt+F |
Move forward one word |
Alt+B |
Move backward one word |
Ctrl+F |
Move forward one character |
Ctrl+B |
Move backward one character |
Editing
| Shortcut | Action |
|---|---|
Ctrl+K |
Delete from cursor to end of line |
Ctrl+U |
Delete from cursor to beginning of line |
Ctrl+W |
Delete the word before the cursor |
Alt+D |
Delete the word after the cursor |
Ctrl+Y |
Paste (yank) previously killed text |
Ctrl+_ |
Undo last change |
Process Control
| Shortcut | Action |
|---|---|
Ctrl+C |
Kill current command |
Ctrl+Z |
Suspend current process to background |
Ctrl+D |
Send EOF / logout of current shell |
Ctrl+L |
Clear the screen (same as clear) |
Practical Shortcuts
# Long command — move to beginning to fix a typo
sudo apt install python3-pip python3-dev python3-venv libssl-dev
# Cursor is at end — press Ctrl+A to jump to start
# Press Alt+F to move forward word by word
# Press Ctrl+W to delete the word before cursor
# Fix a typo in a URL without retyping
curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json"
# Typo in URL — press Ctrl+A then Alt+F to navigate to it
# Or press Ctrl+R and search for the unique part
# Clear the screen without losing the current command
# Type part of a command, then press Ctrl+L — screen clears, your typed command remains
# Quick password entry — type a mistake
sudo vim /etc/nginx/nginx.conf
# Realize you mistyped the path
# Press Ctrl+A to go to start, navigate with Alt+F, fix the path
HISTSIZE and HISTFILESIZE
Control how many commands are saved:
# Current settings
echo "RAM history: $HISTSIZE"
echo "File history: $HISTFILESIZE"
echo "History file: $HISTFILE"
# Defaults: usually 1000 commands in RAM, 2000 in file
# Increase history size (add to ~/.bashrc)
export HISTSIZE=10000 # Keep 10000 commands in RAM
export HISTFILESIZE=20000 # Keep 20000 commands in ~/.bash_history
# Don't save duplicate consecutive commands
export HISTCONTROL=ignoredups
# Don't save both duplicate AND commands starting with space
export HISTCONTROL=ignoreboth
# Tip: prefix any command with a space to keep it out of history:
my-secret-password-command ← won't be saved in history
# Add timestamps to history
export HISTTIMEFORMAT="%Y-%m-%d %H:%M "
# Append to history file (don't overwrite) — for multiple terminal sessions
shopt -s histappend
# Apply all changes
source ~/.bashrc
alias — Create Command Shortcuts
# Create a temporary alias (lasts until terminal is closed)
alias ll='ls -alh --color=auto'
alias gs='git status'
alias k='kubectl'
# See all current aliases
alias
# See a specific alias
alias ll
# alias ll='ls -alh --color=auto'
# Remove an alias
unalias gs
# Make aliases permanent (add to ~/.bashrc)
cat >> ~/.bashrc << 'EOF'
# ── Git Aliases ──────────────────────────────────────────
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph --decorate --all'
alias gd='git diff'
# ── Navigation Aliases ───────────────────────────────────
alias ll='ls -alh --color=auto'
alias la='ls -A'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# ── System Aliases ───────────────────────────────────────
alias update='sudo apt update && sudo apt upgrade -y'
alias ports='ss -tlnp'
alias myip='curl -s ifconfig.me && echo'
alias diskuse='df -h | grep -v tmpfs'
alias memuse='free -h'
# ── Safety Aliases ───────────────────────────────────────
alias rm='rm -i' # Always confirm deletions
alias cp='cp -i' # Always confirm overwrites
alias mv='mv -i' # Always confirm overwrites
EOF
source ~/.bashrc
Key Takeaways
historyshows all past commands with numbers;history | grep keywordfinds specific commands!!repeats the last command;sudo !!is the most-used pattern (forgot sudo)!stringre-runs the most recent command starting with that string!$expands to the last argument of the previous command — great formkdirthencdCtrl+Ris reverse history search — type part of any previous command to find and run itCtrl+A/Ctrl+E— jump to start/end of line;Ctrl+K/Ctrl+U— delete to end/start- Set
HISTSIZE=10000andHISTTIMEFORMATin~/.bashrcto improve history usefulness - Aliases save time — define them in
~/.bashrcandsourcethe file to apply
| ← Back to Getting Help | Next → Practice Exercises |