Background: History is a regular use of Linux in the content, in the work of some users will suddenly find that they can not install a certain software, and then seek the help of operators, and do not show you what he did in the end what the pit father operation. The first thing you need to do at this point is to see the history of the command. After viewing it, you may find out what he did to the pit daddy.
History allows you to quickly locate the problem under certain circumstances.
The history of this article is introduced and its practice comes from CentOS7.2 environment
[Email protected] ~]# cat/etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
Introduction to history
History is the shell's built-in command, which is in the system's default Shell man manual.
History is a command that displays and executes on the terminal, and the system retains 1000 entries by default.
[Email protected] ~]# history
1 ls
2 vi/etc/sysconfig/network-scripts/ifcfg-eno16777728
3 Service Network restart
4 ifconfig
5 Ping www.linuxidc.com
6 Systemctl Disable Firewalld.service
7 Systemctl Stop Firewalld.service
8 exit
9 ls
Ten type which
which LS
File/usr/bin/ls
which clock
File/usr/sbin/clock
Man which
what
The WHO
Who
Who
Man W
Man
Who-q
Max man W
...
..
.
After the system is closed, the existing history content is saved in the file ~/.bash_history
History -related environment variables
HISTFILE Specifies the location of the history file, the default location is ~/.bash_profile (for users), 
/etc/profile (for global, if ~/.bash_ No relevant environment variable content in profile is set using global variable)  
histfilesize command history file records the number of history  
histsize The number of records in the command history, The default is 1000 
histtimeformat= "%F%T" Displays the time when the command occurred  
histignore=" STR1:STR2: ... " Ignore string1,string2 history  
histcontrol contains 4 items, Which entry takes effect only need to make it = The following item can be  
ignoredups: ignores duplicate commands; continuous and the same side is "repeat"  
ignorespace: ignores all commands that begin with whitespace
Ignoreboth:ignoredups,ignorespace 
erasedups: Remove Duplicate command
Let the above environment variables take effect:
1, directly in the current shell input related variables, such as we do not want to retain the command history, we set the histsize to 0
[Email protected] ~]# histsize=0
[Email protected] ~]# history
Tested and successful. However, the limitation of this setting is that its scope is only for the current shell and its child shell, if the switch user or log out and then log in their settings are invalidated. However, it is characterized by the immediate effect of setup.
The following experiment illustrates this problem
[Email protected] ~]# bash
[Email protected] ~]# history
[Email protected] ~]# history
The above results show that after setting the environment variable of history in the current shell, it is scoped to the current shell and the child shell.
Last Login:fri Jul 17:26:41 from 10.1.250.62
[Email protected] ~]# history
1 history
The original history environment variable is invalid after re-landing
2. Another way to make the history environment variable effective is to modify the ~/.bash_profile file
[[email protected] ~]# VI ~/.bash_profile  
#. Bash_profile 
 
# Get the aliases and functions  
If [-f ~/.BASHRC]; then
~/.bashrc 
Fi
 
# User specific Environment and startup Programs 
 
path= $PATH: $HOME/bin
histtimeformat= "%F%T" This is the newly added history environment variable, and I added the time when the command operation occurred.
Export PATH
wq save exit.
 
1 history
2 Ll
3 CD
4 hostory 
5 history
6 VI ~/.bash_profile
7 history
The result shows that the variable is not in effect and we re-login and try again.
[Email protected] ~]# history
1 2016-07-29 20:00:29 History
2 2016-07-29 20:00:29 ll
3 2016-07-29 20:00:29 CD
4 2016-07-29 20:00:29 hostory
5 2016-07-29 20:00:29 History
6 2016-07-29 20:00:29 VI ~/.bash_profile
7 2016-07-29 20:00:29 History
8 2016-07-29 20:00:29 Logout
9 2016-07-29 20:00:33 History
With the above two results, we can find that the second modification configuration file can also be successfully modified by the history environment variable but it will need to be re-landed, not after the change. But it is characterized by this modification is always valid, timeliness is permanent.
use of the history command
- C: empty command history
- D N: Deletes the command specified in history, n indicates the command number
#: Show Recent # History
- A: Append the newly executed command history list to the history file, because multiple terminals so if you want to see what is happening now, you can do a view
- N: Read unread rows to History list (memory data) in history file (Local data)
- R: Read History file (local data) append to History list (memory data)
- W: Saves the History list (memory data) to the specified history file (local data)
- S: expands the history parameter into a row, appended to the history list. Used to forge command history
Here are some examples of the use of the above commands
[Email protected] ~]# history
1 2016-07-29 20:00:29 History
2 2016-07-29 20:00:29 ll
3 2016-07-29 20:00:29 CD
4 2016-07-29 20:00:29 Hostory 
5 2016-07-29 20:00:29 history
6 2016-07-29 20:00:29 vi ~/.bash_profile  
7 2016-07-29 20:00:29 history
8 2016-07-29 20:00:29 logout
9 2016-07-2 9 20:00:33 History
10 2016-07-29 20:07:41 cd
11 2016-07-29 20:07:44 ls
& nbsp 12 2016-07-29 20:07:50 History
13 2016-07-29 20:08:12 cat/etc/profile
14 2016-07-29 20:12:10 histcontrol=ignorespace 
15 2016-07-29 20:27:28 history-p 
16 2016-07-29 20:27:31 History
17 2016-07-29 20:28:10 ls/etc
18 2016-07-29 20:28:14 history
19 2016-07-29 20:28:57 history
Clear History
[Email protected] ~]# history-c
[Email protected] ~]# history
2016-07-29 20:29:26 History
command history before reading from a history file
[Email protected] ~]# History-r
[Email protected] ~]# history
1 2016-07-29 20:29:26 History
2 2016-07-29 20:30:59 history-r
3 2016-07-29 20:30:59 History
4 2016-07-29 20:30:59 ll
5 2016-07-29 20:30:59 CD
6 2016-07-29 20:30:59 hostory
7 2016-07-29 20:30:59 History
8 2016-07-29 20:30:59 VI ~/.bash_profile
9 2016-07-29 20:30:59 History
Ten 2016-07-29 20:30:59 Logout
2016-07-29 20:31:01 History
Delete the specified command history
[[email protected] ~]# history-d 4  
[[email protected] ~]# history  
1 2016-07-29 20:29:26 history
2 2016-07-29 20:30:59 history-r  
3 2016-07-29 20:30:59 History
4 2016-07-29 20:30:59 CD
5 2016-07-29 20:30:59 H Ostory 
6 2016-07-29 20:30:59 history
7 2016-07-29 20:30:59 vi ~/.bash_profile  
8 2016-07-29 20:30:59 history
9 2016-07-29 20:30:59 logout
10 2016-07 -29 20:31:01 History
11 2016-07-29 20:32:50 history-d 4 
12 2016-07-29 20:32:52 history
forge A History order
12345678910111213141516 [[email protected] ~]# history-s RM-RF/* do a prank
[Email protected] ~]# history
1 2016-07-29 20:29:26 History
2 2016-07-29 20:30:59 History-r 
3 2016-07-29 20:30:59 history
4 2016-07-29 20:30:59 CD
5 2016-07-29 20:30:59 hostory &NBSP
6 2016-07-29 20:30:59 history
7 2016-07-29 20:30:59 VI ~/.bash_profi Le  
8 2016-07-29 20:30:59 history
9 2016-07-29 20:30:59 logout
10 2016-07-29 20:31:01 history
11 2016-07-29 20 : 32:50 history-d 4 
12 2016-07-29 20:32:52 history
13 2016-07-29 20:33:57 Rm-rf/bin/boot/dev/etc/home/lib/lib64/media/mnt/opt/proc/root/run/sbin /srv/sys/tmp/usr/var
14 2016-07-29 20:34:00 history
I believe that anyone who enters the history to see this command will be scared to sweat.
Call history parameters in detail
#cmd!^: Use the first parameter of the previous command to make a cmd argument
#cmd!$: Use the last parameter of the previous command to make a cmd argument
#cmd!*: Use all parameters of the previous command to do cmd parameters
#cmd!:n: Parameters that use the nth parameter of the previous command to do a cmd
#!n: Invoke nth command 
#!-n: Call the reciprocal nth command  
#!!: Executes the previous command  
#!$: Reference the last parameter of the previous command with the key combination Esc,. #!n:^ invokes the first parameter of the nth command  
#!n:$ invokes the last parameter of the nth command  
#!m:n invokes the nth parameter of the M command  
#!n:* Invokes all arguments of the nth command  
#!string: Executes the last command in the command history that starts with the specified string  
#!string:^ searches the command history for a command that begins with a string and gets its first argument #!string:$ searches the command history for a command that begins with a string and obtains its last argument  
#!string:n From the command history, search for a command starting with string and get its nth parameter  
#!string:* from the command calendar?? Searches for a command that begins with a string and gets all its arguments
The following experiment is done to practice the specific usage of the above historical parameters
[Email protected] ~]# history
1 2016-07-29 20:29:26 History
2 2016-07-29 20:30:59 History-r
3 2016-07-29 20:30:59 History
4 2016-07-29 20:30:59 CD
5 2016-07-29 20:30:59 Hostory
6 2016-07-29 20:30:59 History
7 2016-07-29 20:30:59 VI ~/.bash_profile
8 2016-07-29 20:30:59 History
9 2016-07-29 20:30:59 Logout
Ten 2016-07-29 20:31:01 History
2016-07-29 20:32:50 history-d 4
2016-07-29 20:32:52 History
2016-07-29 20:33:57 rm-rf/bin/boot/dev/etc/home/lib/lib64/media/mnt/opt/proc/root/run/sbin/srv/sys/t Mp/usr/var
2016-07-29 20:34:00 History
2016-07-29 20:40:32 ls
2016-07-29 20:40:33 CD
2016-07-29 20:40:45 ls/etc/passwd
2016-07-29 20:41:35 History
We use first!! To invoke the previous command
[[email protected] ~]#!!
History
1 2016-07-29 20:29:26 History
2 2016-07-29 20:30:59 History-r
3 2016-07-29 20:30:59 History
4 2016-07-29 20:30:59 CD
5 2016-07-29 20:30:59 Hostory
6 2016-07-29 20:30:59 History
7 2016-07-29 20:30:59 VI ~/.bash_profile
8 2016-07-29 20:30:59 History
9 2016-07-29 20:30:59 Logout
Ten 2016-07-29 20:31:01 History
2016-07-29 20:32:50 history-d 4
2016-07-29 20:32:52 History
2016-07-29 20:33:57 rm-rf/bin/boot/dev/etc/home/lib/lib64/media/mnt/opt/proc/root/run/sbin/srv/sys/t Mp/usr/var
2016-07-29 20:34:00 History
2016-07-29 20:40:32 ls
2016-07-29 20:40:33 CD
2016-07-29 20:40:45 ls/etc/passwd
2016-07-29 20:41:35 History
[Email protected] ~]#!h
History
1 2016-07-29 20:29:26 History
2 2016-07-29 20:30:59 History-r
3 2016-07-29 20:30:59 History
4 2016-07-29 20:30:59 CD
5 2016-07-29 20:30:59 Hostory 
6 2016-07-29 20:30:59 history
7 2016-07-29 20:30:59 vi ~/.bash_profile  
8 2016-07-29 20:30:59 history
9 2016-07-29 20:30:59 logout
10 2016-07-29 20:3 1:01 history
11 2016-07-29 20:32:50 history-d 4 
12 2016-07-29 20:32:52 History
13 2016-07-29 20:33:57 rm-rf/bin/boot/dev/etc/home/lib/lib64 /media/mnt/opt/proc/root/run/sbin/srv/sys/tmp/usr/var
14 2016-07-29 20:34:00 history
15 2016-07-29 20:40:32 ls
16 2016-07-29 20:40:33 cd
17 2016-07-29 20:40:45 ls/etc/p ASSWD
18 2016-07-29 20:41:35 history
19 2016-07-29 20:47:03 history
20 2016-07-29 20:48:22 history
We are interested in one experiment. This article is introduced here.
Common shortcut keys
Recall the last parameter in the previous command:
!$
ESC,. (Click the ESC key and then release and click.) Key
These two are very common, especially ESC,.
After we create a file, we usually make modifications or other reads, and when you type the command, you can quickly fill in the required commands by using the shortcut keys described above.
A detailed description of the history of Linux Basics