Complete Guide to the Linux history Command
history is an extremely useful built-in command in Linux systems. It records all the command history that users have executed in the terminal. This feature is especially valuable in the following scenarios:
- Trace operations: View previously executed commands
- Quick reuse: No need to retype long commands
- Troubleshooting: Check system operation records
- Efficiency improvement: Quickly complete repetitive tasks using historical commands
When you enter commands in the terminal, Bash saves them in memory and writes them to~/.bash_historythe file (under default configuration).
Basic Syntax and Usage
The basic syntax of the history command is very simple:
history [选项] [参数]
Common Forms
1. View the complete history:
history
2. View the most recent N records:
history 10 # 显示最近10条命令
3. Clear all history:
history -c
4. Delete a specific history record (e.g., delete entry 1010):
history -d 1010
Detailed Explanation of Common Options and Parameters
The history command supports several useful options:
| Option | Description | Example |
|---|---|---|
-c |
Clear all history | history -c |
-d |
Delete the history record at the specified position | history -d 1005 |
-a |
Immediately write history from memory to the history file | history -a |
-n |
Read unread history records from the history file | history -n |
-r |
Read the contents of the history file into the current session | history -r |
-w |
Write the current history to the history file | history -w |
Practical Tips and Advanced Usage
1. Quickly Execute Historical Commands
Example
!! # Execute the previous command
!string # Execute the most recent command starting with string
Example:
Example
!! # Re-execute the previous command
!vim # Execute the most recent command starting with vim
2. Search Historical Commands
UsingCtrl+Ryou can reverse search history commands; enter a partial keyword to find the matching command.
3. Historical Command Substitution
Example
Example:
Example
$ ^file1^file2 # Equivalent to executing cat file2.txt
4. Display Command Timestamps
In~/.bashrcAdd the following configuration to display command execution time:
Example
Then execute:
Example
After thathistorythe command will display the execution time of each command.
Environment Variable Configuration
The behavior of the history command can be customized through environment variables:
| Variable | Description | Recommended Value |
|---|---|---|
HISTSIZE |
Number of history commands kept in memory | 5000 |
HISTFILESIZE |
Number of commands kept in the history file | 10000 |
HISTCONTROL |
Controls the way history is recorded | ignoredups:erasedups |
HISTIGNORE |
Specifies commands not to record | "ls:cd:pwd:exit" |
Configuration example (add to~/.bashrc):
Example
export HISTFILESIZE=10000
export HISTCONTROL=ignoredups:erasedups
export HISTIGNORE="ls:cd:pwd:exit"
export HISTTIMEFORMAT="%F %T "
Practical Application Scenarios
Scenario 1: Recover Forgotten Commands
Example
Scenario 2: Count the Most Frequently Used Commands
Example
Scenario 3: Back Up History Records
Example
cp ~/.bash_history ~/command_history_backup_$(date +%F).txt
Notes
- Privacy and security: Historical records may contain sensitive information (e.g., passwords); protect them accordingly
- Multiple terminal issue: Different terminal sessions do not share history in real time by default
- History loss: Abnormal exit may cause commands not to be saved
- Large file handling: Excessively large history files may affect performance
Summary and Exercises
Review your command history and find the 5 most recently used git commands
history | grep git | tail -5
Configure your bash environment so that history records include timestamps and ignore duplicate commands
Create an alias to quickly back up the current history to a specified directory
alias backup_history='cp ~/.bash_history ~/history_backups/history_$(date +%Y%m%d_%H%M%S).txt'
Try using
Ctrl+Rto search for a complex command you have used before.
By mastering the history command, you can significantly improve your efficiency in the Linux terminal, reduce redundant input, and better manage your command-line operation history.
Other Extensions
Linux Command Encyclopedia