History command usage
History is a very practical shell command that can display the commands run in the shell before. With the last command, you can trace which user has executed some commands. The following describes the Common commands or techniques used in history. In actual system management, proper application can help you get twice the result with half the effort.
Use HISTTIMEFORMAT to display TIMESTAMP in history
By default, history does not record the command execution time, which is difficult to analyze.
# At this time, you can configure the HISTTIMEFORMAT environment variable to add a timestamp to the history record.
Export HISTTIMEFORMAT = "% F % T"
Add the above export command ~ /. In bashrc, The history timestamp is automatically enabled after logon.
In fact, the format of HISTTIMEFORMAT is the format of the strftime function, such as the above "% F % T", % F Represents the date that shows the Y-M-D format, % T represents the time that shows the format of H-M-S. For more formats, refer to man strftime.
Use Ctrl + R to search for historical commands
I use the history feature most often. After you execute a long string of commands, you only need to search for the history command with the keyword and then re-execute the command without re-entering the entire command. Press Ctrl + R and enter a keyword. In the following example, I search for "cat" to display the command "cat anaconda-ks.cfg" containing "cat" in previous commands ". Press enter to re-execute this command, and press the left or right arrow keys to directly modify the command. After the command is changed, press enter to execute. This is very convenient!
[Note: press Ctrl + R at the command line prompt. The terminal displays the following prompt: reverse-I-search]
Use HISTSIZE to control the total number of historical commands
# The number of history entries in the memory. It will be written to ~ after logging out ~ /. Bash_history File
Export HISTSIZE = 450
# Maximum number of history lines stored in the file. File :~ /. Bash_history
Export HISTFILESIZE = 450
# If you want to disable history and do not allow shell to record commands, set HISTSIZE to 0.
Export HISTSIZE = 0
Use HISTFILE to change the historical file name
By default, the command history is stored in. in the bash_history file, add the following line. in the bash_profile file, log on to the shell again, and replace the new file. the bash_history file is used to store historical commands. You can use this command to track commands executed on different terminals. At that time, you only need to save the commands executed on different terminals in different historical files.
[Root @ rocket ~] # Vim./. bashrc
HISTFILE =/root/my_history
Log on again and you will see that the new history file has taken effect.
Use HISTCONTROL to remove consecutive duplicate entries in the command history
Export HISTCONTROL = ignoredups
Use HISTCONTROL to remove duplicate commands throughout history
Export HISTCONTROL = erasedups
Clear all History commands with option c
# Clear previous commands
History-c
Use HISTIGNORE to make history ignore certain commands during storage
Sometimes you don't want to see Basic commands such as "pwd" and "ls" in the record. You can use HISTIGNORE to ignore these commands.
Note that "ls" is added to HISTIGNORE, and "ls-l" is ignored only ". Be sure to accurately write the commands to be ignored.
Export HISTIGNORE = "pwd: ls: history"
Customized output formats of history in Linux
History introduction and quick calling of bash commands
Linux history command details [graphic]
Linux history commands
This article permanently updates the link address: