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 a user has executed in the terminal. This feature is particularly valuable in the following scenarios:
- Tracing operations: View previously executed commands
- Quick reuse: No need to retype long commands
- Troubleshooting: Check system operation records
- Efficiency Improvement: Quickly complete repetitive work through history commands
When you enter commands in the terminal, Bash saves these commands in memory and writes them to the ~/.bash_history file upon exit (under the 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 (for example, delete record 1010):
history -d 1010
Detailed explanation of common options
The history command supports several useful options:
| Option | Description | Example |
|---|---|---|
-c |
Clear all history | history -c |
-d |
Delete a history record at a specified position | history -d 1005 |
-a |
Immediately write the in-memory history to the history file | history -a |
-n |
Read unread history 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 history commands
Example
!
n
Execute the nth command in the history
!!
Execute the previous command
!
string
Execute the most recent command starting with string
Example:
Example
!
1024
Execute the command numbered 1024 in the history
!!
Re-execute the previous command
!
vim
Execute the most recent command starting with vim
2. Search history commands
Use Ctrl+R You can search history commands in reverse; entering part of a keyword is enough to find a matching command.
3. History command substitution
Example
^old^new
Replace old with new in the previous command and execute it
Example:
Example
$
cat
file1.txt
$ ^file1^file2
Equivalent to executing cat file2.txt
4. Display command timestamps
in ~/.bashrc Adding the following configuration to it can display the command execution time:
Example
export
HISTTIMEFORMAT
=
"%F %T "
Then execute:
Example
source
~
/
.bashrc
after history The command will display the execution time of each command.
Environment variable configuration
The behavior of the history command can be customized through environment variables:
| Variables | Description | Recommended value |
|---|---|---|
HISTSIZE |
Number of history commands kept in memory | 5000 |
HISTFILESIZE |
Number of commands kept in the history file | 10000 |
HISTCONTROL |
Control how history is recorded | ignoredups:erasedups |
HISTIGNORE |
Specify commands not to be recorded | "ls:cd:pwd:exit" |
Configuration example (add to ~/.bashrc):
Example
export
HISTSIZE
=
5000
export
HISTFILESIZE
=
10000
export
HISTCONTROL
=ignoredups:erasedups
export
HISTIGNORE
=
"ls:cd:pwd:exit"
export
HISTTIMEFORMAT
=
"%F %T "
Practical Application Scenarios
Scenario 1: Recover a forgotten command
Example
history
|
grep
"apt install"
Scenario 2: Count the most used commands
Example
history
|
awk
'{CMD[$2]++;count++;} END {for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}'
|
grep
-v
"./"
|
column
-c3
-s
" "
-t
|
sort
-nr
|
nl
|
head
-n10
Scenario 3: Back up history
Example
history
-a
Make sure the latest commands have been written to the file
cp
~
/
.bash_history ~
/
command_history_backup_$
(
date
%
F
)
.txt
Notes
- Privacy and security: History may contain sensitive information (such as passwords), so be careful to protect it
- Multiple terminal issues: Different terminal sessions do not share history in real time by default
- History loss: An abnormal exit may cause commands not to be saved
- Large file processing: An overly large history file may affect performance
Summary and exercises
-
View your command history and find the 5 most recently used git commands
history | grep git | tail -5 2.
Configure your bash environment so that history includes timestamps and ignores duplicate commands
3.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'4.
Try using Ctrl+R Search for a complex command you used before
By mastering the history command, you can significantly improve your efficiency in the Linux terminal, reduce repetitive typing, and better manage your command-line operation history.
Linux Command Reference