What is history
? ? In the cumulative use of Linux systems, we will enter many commands. And when we want to repeat the last command, we can view the commands we have entered and used by using the arrow keys to flip up. Have you ever thought about where this command was saved and how many records will be saved? Today we are going to learn the history order.
? ? You can tell from history that the command is used primarily to view commands that the user has entered and used. Use this command to reduce the number of times we routinely repeat input commands. The common options are as follows:
| Options |
Description |
| -C |
Clear History Command |
| -D Offset |
Delete the history command at the specified location |
| -A |
Add a New History command record |
| -R |
Read History from Historyfile as the current history |
| -W |
Saves and overwrites the current history in Historyfile |
Common usage repeats the last execution command
? ? In the daily use of the process, the most common scenario is = = repeatedly executed the last command = =, for this scenario, I think we should use the arrow keys up or down to roll to achieve it. However, there are 4 ways to achieve this scenario, respectively:
- Using the arrow keys to roll up or down, press ENTER
- Enter !!, press the ENTER key
- Enter !-1, press ENTER
- Enter ctrl+p, press ENTER
Repeatedly executes the specified command
? ? In the daily use process, we don't just want to execute the last command, but when we want to execute the specified history command, there are two ways
- Specify the ID, you can use ! Historyid, if we want to execute a command with a Historyid of 1993, you can enter it as follows
[[email protected] ~]# !1993
- Specify the command for fuzzy query, you can use ! Historycmd, = = the command searched here is searched forward from the last history, and the command that satisfies the condition is executed = =, as shown in the following example:
[[email protected] ~]# cat -n ~/.bash_history | grep du 562 du -ah --exclude="*/.*" . 564 du -ah . 565 du -ah . | more 566 du -ah --exclude="*/.*" . 567 du -h --max-depth=1 / 568 du -h --max-depth=1 / | sort -hr[[email protected] ~]# !du#找到离当前最近执行的命令du -h --max-depth=1 / | sort -hr
Search for commands that have been executed
? ? In window, if you want to search for files commonly used shortcut keys ctrl+f, and Linux search for once executed commands use shortcut keys ctrl+r
# 在当前Shell中按Ctrl+R,而后输入命令即可(reverse-i-search)`du‘: cat -n ~/.bash_history | grep du
Add a timestamp to a history command
# 预先定义一个环境变量[[email protected] ~]# export HISTTIMEFORMAT=‘ %F %T ‘[email protected] ~]# history 1009 2018-08-14 23:18:22 pidof sshd 1010 2018-08-14 23:18:22 pidof -o 1995 1011 2018-08-14 23:18:22 pidof sshd -o 1995
Clear History Command
? ? Although the historical command facilitates and reduces the time required to enter execution commands daily, there are also security implications. If you accidentally enter a password in the history command to connect other Linux addresses, MySQL and so on. Once the system has been hacked or seen by non-administrators, there may be serious pitfalls, and you will certainly think of clearing the history command. You can then use the-C option
[[email protected] ~]# history -c
? ? Do you really want to empty the history command by using the following options? The answer is certainly not. What is the reason why the history command still exists after the next login?
-c 选项只是暂时把Linux系统内存中的历史命令给清空,当用户退出时,就不会有历史命令记录追加到.bash_history中,当用户重新登录后,系统会重新加文件.bash_history,而历史命令便又会重新显示出来了。
? ? The practice of actually completely emptying the history command is as follows:
[[email protected] ~]# history -c ; history -w
- Method Two: Empty the ~/.bash_history file
[[email protected] ~]# echo "" > ~/.bash_history
History configuration
[[email protected] ~]# export HISTTIMEFORMAT=‘ %F %T ‘
- Control the number of history command records
#设置内存中保存的命令记录个数[[email protected] ~]# export HISTSIZE=300# 设置.bash_history中保存的命令记录个数[[email protected] ~]# export HISTFILESIZE=300
- Change the default save file
? ? The system default configuration saves the history command to ~/.bash_history , and if you want to save to a different file, you can change it as follows
[[email protected] ~]# export HISTFILE=~/.historycmd.txt
# 清除整个命令历史记录中的重复记录[[email protected] ~]# export HISTCONTROL=erasedups# 忽略命令历史记录中连续重复的记录[[email protected] ~]# export HISTCONTROL=ignoredups# 忽略命令历史记录中以空格开始的记录[[email protected] ~]# export HISTCONTROL=ignorespace# 忽略命令历史记录的重复记录和以空格开始的记录[[email protected] ~]# export HISTCONTROL=ignoreboth
This article is posted on the subscription number, such as your friends like my article, you can also follow my subscription number: Woaitest, or scan the following QR code to add attention:
Linux Basic Tutorial 44-history command