Linux Command: history
Display or manipulate command history
history displays the command history list, showing previously executed commands with their line numbers. It allows you to search, recall, and re-execute past commands, dramatically increasing shell productivity. Bash stores command history in memory during a session and saves it to ~/.bash_history on exit. The HISTSIZE variable controls how many commands are kept in memory, and HISTFILESIZE controls the file size. history supports searching (Ctrl+R), re-execution (!N, !!), and editing. Mastering history navigation is one of the biggest productivity gains for Linux users.
Syntax
history [OPTION]... [N]Key Options
Nโ Show last N commands-cโ Clear the history list-d Nโ Delete entry number N-wโ Write history to file-rโ Read history from file-aโ Append new entries to history file
Examples
Show recent commands
history 20Output: 151 git status\n 152 git add .\n 153 git commit -m "fix"
Search history
history | grep sshOutput: 45 ssh user@server1\n 89 ssh admin@server2
Re-execute last command
!!Pro Tips
- Press Ctrl+R and type to search backwards through history. Press Ctrl+R again to find older matches. Enter to execute, Ctrl+G to cancel.
- Forgot sudo? Type sudo !! to re-run the last command with sudo. !! is replaced with the previous command.
- Commands with passwords appear in history. Use HISTCONTROL=ignorespace and prefix sensitive commands with a space to exclude them.
Learn more: Full history reference โ
Related Resources