Introduction / Why This Matters
The history command is one of the most powerful tools for boosting productivity in the Linux command line. It records every command you've entered, allowing you to:
- Quickly find and repeat previously used complex commands without retyping.
- Analyze your actions, for example, to document routine operations.
- Manage your sessions by clearing unnecessary or confidential entries.
This guide will transform your command history from a simple log into a convenient tool for search and automation.
Prerequisites / Preparation
- Terminal access (Ctrl+Alt+T or similar).
- Shell: The instructions primarily target
bash(standard in most distributions), but most principles apply tozshas well. - Write permissions in your home directory (files
~/.bash_historyor~/.zsh_history). - A text editor (e.g.,
nanoorvim) for editing configuration files.
Step 1: Viewing and Searching Your History
1.1. Basic Viewing
Simply type the command to see a list of all saved commands with line numbers:
history
By default, it displays the last 500 or 1000 entries (depending on the HISTSIZE setting).
1.2. Searching by Content
To find a command containing specific text (e.g., systemctl), use grep:
history | grep systemctl
This filters only the lines where systemctl appears.
1.3. Searching with Keyboard Shortcuts (Interactive Mode)
Ctrl + R— reverse search. Start typing part of a command, and the terminal will show the first match. PressCtrl + Ragain to cycle through others.Enterto execute,Ctrl + Gto exit search.↑/↓— scroll through recent commands.
Step 2: Repeating and Editing Past Commands
2.1. Quick Re-execution
!!— execute the last command. Useful aftersudo(e.g.,sudo !!).!-n— execute the command that wasnsteps back.!-2is the second-to-last.!<string>— execute the last command that starts with<string>. For example,!sysruns the most recent command starting withsys.
2.2. Editing Before Execution
- After finding a command via
Ctrl + R, press←or→to exit search mode and edit the line. - Or, find a command with
↑/↓arrows and edit it like a normal input line.
Step 3: Configuring History Size and Format
By default, your history may be limited. Let's change that.
3.1. Increasing the Number of Saved Commands
Open your shell's configuration file. For bash, this is typically ~/.bashrc:
nano ~/.bashrc
Add or modify the following lines at the end of the file:
# Maximum number of commands in session memory
HISTSIZE=5000
# Maximum number of commands in the history file
HISTFILESIZE=10000
Save (Ctrl+O, Enter) and close (Ctrl+X) the editor. To apply changes in the current session, run:
source ~/.bashrc
3.2. Adding Command Execution Timestamps
For auditing or analysis, it's useful to see when a command was run. Add to ~/.bashrc:
# Format: Year-Month-Day Hour:Minute:Second
export HISTTIMEFORMAT='%F %T '
After source ~/.bashrc, the history command will display the timestamp before each command.
💡 Tip: For
zsh, use theHISTTIMEFORMATvariable similarly or configure viasetopt EXTENDED_HISTORY.
Step 4: Clearing and Managing the History File
4.1. Clearing the Current Session (RAM)
history -c
This removes all entries from the current session but does not affect the ~/.bash_history file on disk.
4.2. Clearing the History File on Disk
To erase history in the file as well, run two commands:
history -c # 1. Clear session memory
history -w # 2. Write (empty) session memory to ~/.bash_history
Warning: This action is irreversible for the current user.
4.3. Deleting a Specific Entry
- Find the line number of the command via
history. - Delete it:
history -d <line_number> - Immediately sync with the file:
history -w
4.4. Disabling History Recording (Temporarily)
For confidential operations (e.g., typing passwords in plain text, which is highly discouraged), you can temporarily disable recording:
set +o history # Disable recording for current session
# ... perform confidential commands ...
set -o history # Re-enable recording
Important: Commands entered while disabled won't appear in history, but they may still be visible in process lists (ps aux) or the terminal's scrollback buffer.
Step 5: Useful Advanced Techniques
5.1. Executing a History Command by Number
!<number>
For example, !123 executes command number 123.
5.2. Substituting Arguments from the Previous Command
!$— the last argument of the previous command. Useful for operations on the same file:cp file.txt backup.txt && mv !$ new_name.txt.!*— all arguments except the first command.
5.3. Exporting History to a File for Analysis
history > my_history.txt
Now you have a text file with the full history for searching or documentation.
Verification
- Run several different commands (
ls,pwd,echo "test"). - Type
history— they should appear. - If you configured
HISTTIMEFORMAT, verify timestamps are displayed. - Try searching with
Ctrl+R— it should find your commands. - Check the history file size:
ls -la ~/.bash_history.
If commands appear in history output and persist after closing and reopening the terminal — the configuration is working correctly.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| History is empty after terminal restart | HISTSIZE is set to 0 in ~/.bashrc or ~/.profile. | Check these files for HISTSIZE=0 and correct it. |
Commands run via sudo are not saved | History is recorded per user by default. Commands executed with sudo may be logged in root's history (/root/.bash_history). | To log sudo commands in your history, configure sudo (option timestamp_timeout), but this may be insecure. |
| History "jumps" or loses entries | The ~/.bash_history file is corrupted or has incorrect permissions. | Ensure you own the file: ls -la ~/.bash_history. If needed, delete the file (it will be recreated) and reconfigure HISTFILESIZE. |
history -w does not write changes | You are in a su or sudo su session. The history file is being written as root. | Exit the root session (exit) or configure HISTFILE for the root session. |
Frequently Asked Questions (FAQ)
Can I synchronize history between multiple terminal windows?
Yes, but with caveats. By default, each shell session reads history at startup and writes it on exit. When working in multiple windows simultaneously, recent commands from one window won't appear in another until you exit the first session. To get live updates, you can manually use history -a (append current commands to file) and history -n (read new lines from file), or set up PROMPT_COMMAND in ~/.bashrc:
export PROMPT_COMMAND='history -a; history -n'
How do I find and run a command that contained a typo?
Use search based on the part you remember. For example, if you meant to type git commit but typed git comit, search via history | grep comit, get the line number, and run !<number> or simply find it with arrows and correct it.
Is it safe to store all history?
No. History is a plain-text file (~/.bash_history), readable by anyone with access to your account. Never type passwords, API keys, or other secrets into the terminal. For confidential operations, disable history (set +o history) or use specialized tools (e.g., ssh-keygen doesn't require password input in history).
Why does history show fewer lines than I expect?
Check two variables: echo $HISTSIZE (limit for current session) and echo $HISTFILESIZE (limit for the file). Also ensure you didn't clear history earlier. The ~/.bash_history file might be hidden or in a non-standard location (check echo $HISTFILE).