Linux basics-history
Background: history is often used in Linux. At work, some users suddenly find that they cannot install a software, so they seek help from O & M personnel, instead of showing you what he has done. In this case, you must first check the history of the history command. Maybe you will find out what he has done.
History allows you to quickly locate the problem under certain circumstances.
The introduction and practice of history in this article are from the centos environment.
[Root @ localhost ~] # Cat/etc/RedHat-release
CentOS Linux release 7.2.1511 (Core)
History Introduction
History is a built-in shell command. Its content is in the man manual of the default shell.
History is the command entered and executed on the terminal. The system retains 1000 records by default.
[Root @ localhost ~] # History
1 ls
2 vi/etc/sysconfig/network-scripts/ifcfg-eno16777728
3 service network restart
4 ifconfig
5 ping www.bkjia.com
6 systemctl disable firewalld. service
7 systemctl stop firewalld. service
8 exit
9 ls
10 type which
11 which ls
12 file/usr/bin/ls
13 which clock
14 file/usr/sbin/clock
15 man which
16 man what
17 man who
18 who
19 man who
20 man w
21 man who
22 who-q
23 man w
...
..
.
After the system shuts down, it will save the existing history content in the file ~ /. Bash_history
History-related environment variables
HISTFILE specifies the location where historical files are stored. The default location is ~ /. Bash_profile (for users ),
/Etc/profile (for global, if ~ /. Use global variable settings if there is no related environment variable in bash_profile)
The number of historical records in the history file of the HISTFILESIZE command.
The number of history records of the HISTSIZE command. The default value is 1000.
HISTTIMEFORMAT = "% F % T" display the time when the command occurred
HISTIGNORE = "str1: str2:..." ignore string1, string2 history
HISTCONTROL contains four items. To make this item take effect, you only need to make it = the following one.
Ignoredups: Ignore duplicate commands; continuous and same as "DUPLICATE"
Ignorespace: Ignore all commands starting with blank
Ignoreboth: ignoredups, ignorespace
Erasedups: delete duplicate commands
How to make the preceding environment variables take effect:
1. directly input relevant variables in the current shell. For example, if we do not want to retain the command history, we set HISTSIZE to 0.
[Root @ localhost ~] # HISTSIZE = 0
[Root @ localhost ~] # History
Tested. However, the limitation of this setting is that it only applies to the current shell and its sub-shell. If you switch the user or log out and then log on to it, the setting is invalid. However, the setting takes effect immediately.
This problem is explained through experiments below
[Root @ localhost ~] # Bash
[Root @ localhost ~] # History
[Root @ localhost ~] # History
The above results indicate that after the history environment variable is set in the current shell, the function range is the current shell and sub-shell.
Last login: Fri Jul 29 17:26:41 2016 from 10.1.250.62
[Root @ localhost ~] # History
1 history
The original history environment variable becomes invalid after you log on again.
2. Another way to make the history environment variables take effect is to modify ~ /. Bash_profile File
[Root @ localhost ~] # 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. I added the time when the command operation occurred.
Export PATH
Wq is saved and exited.
[Root @ localhost ~] # History
1 history
2 ll
3 cd
4 hostory
5 history
6 vi ~ /. Bash_profile
7 history
The result shows that the variable does not take effect. We can log on again and try again.
[Root @ localhost ~] # History
1 2016-07-29 20:00:29 history
2 2016-07-29 20:00:29 ll
3 20:00:29 cd
4 2016-07-29 20:00:29 hostory
5 2016-07-29 20:00:29 history
6 20:00:29 vi ~ /. Bash_profile
7 20:00:29 history
8 2016-07-29 20:00:29 logout
9 20:00:33 history
Based on the above two results, we can find that although the configuration file of the second type can be modified successfully, but it takes effect and needs to be logged on again. However, this modification is always effective and its timeliness is permanent.
Use of the history command
-C: Clear command history
-D n: Delete the specified command in the history. n indicates the command number.
#: Display the latest # History
-A: append the history list of new commands executed in this session to the history file. Because multiple terminals are used, you can execute-a to view all the commands currently executed.
-N: read history file (local data) unread row to history list (memory data)
-R: attaches the read history file (local data) to the History List (memory data)
-W: Save the Historical List (memory data) to the specified historical file (local data)
-S: Expand the history parameter into a row and append it to the history list. Used to forge command history
The following shows how to use the preceding commands.
[Root @ localhost ~] # History
1 2016-07-29 20:00:29 history
2 2016-07-29 20:00:29 ll
3 20:00:29 cd
4 2016-07-29 20:00:29 hostory
5 2016-07-29 20:00:29 history
6 20:00:29 vi ~ /. Bash_profile
7 20:00:29 history
8 2016-07-29 20:00:29 logout
9 20:00:33 history
10 20:07:41 cd
11 2016-07-29 20:07:44 ls
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 20:27:31 history
17 2016-07-29 20:28:10 ls/etc
18 20:28:14 history
19 2016-07-29 20:28:57 history
Clear history
[Root @ localhost ~] # History-c
[Root @ localhost ~] # History
20:29:26 history
READ command history from history files
[Root @ localhost ~] # History-r
[Root @ localhost ~] # 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 20:30:59 cd
6 20:30:59 hostory
7 20:30:59 history
8 20:30:59 vi ~ /. Bash_profile
9 20:30:59 history
10 2016-07-29 20:30:59 logout
11 20:31:01 history
Deletes the specified command history.
[Root @ localhost ~] # History-d 4
[Root @ localhost ~] # 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 20:30:59 cd
5 20:30:59 hostory
6 20:30:59 history
7 20:30:59 vi ~ /. Bash_profile
8 2016-07-29 20:30:59 history
9 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
History forgery commands
12345678910111213141516 [root @ localhost ~] # History-s rm-rf/* prank
[Root @ localhost ~] # 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 20:30:59 cd
5 20:30:59 hostory
6 20:30:59 history
7 20:30:59 vi ~ /. Bash_profile
8 2016-07-29 20:30:59 history
9 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 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 anyone who enters history will be scared to see this command.
Detailed explanation of call history parameters
# Cmd! ^: Use the first parameter of the previous command as the cmd parameter.
# Cmd! $: Use the last parameter of the previous command as the cmd parameter.
# Cmd! *: Use all the parameters of the previous command as cmd parameters.
# Cmd! : N: Use the nth parameter of the previous command as the cmd parameter.
#! N: Call the nth command
#! -N: Call the nth command.
#!! : Execute the previous command
#! $: Reference the last parameter of the previous command ,.
#! N: ^ The first parameter to call the nth command
#! N: $ call the last parameter of the n command
#! M: n call the nth parameter of the MTH command
#! N: * Call all parameters of command n
#! String: The last command in the execution history that starts with a specified string.
#! String: ^ search for a command starting with string from the command history and obtain its first Parameter
#! String: $ search for a command starting with string from the command history and obtain its last parameter
#! String: n search for a command starting with string from the command history and obtain its nth Parameter
#! String: * search for a command starting with string from the command history and obtain all its parameters.
The following describes how to use the previous parameters through experiments.
[Root @ localhost ~] # 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 20:30:59 cd
5 20:30:59 hostory
6 20:30:59 history
7 20:30:59 vi ~ /. Bash_profile
8 2016-07-29 20:30:59 history
9 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 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 20:40:32 ls
16 20:40:33 cd
17 2016-07-29 20:40:45 ls/etc/passwd
18 20:41:35 history
Let's use it first !! To call the previous command.
[Root @ localhost ~] #!!
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 20:30:59 cd
5 20:30:59 hostory
6 20:30:59 history
7 20:30:59 vi ~ /. Bash_profile
8 2016-07-29 20:30:59 history
9 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 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 20:40:32 ls
16 20:40:33 cd
17 2016-07-29 20:40:45 ls/etc/passwd
18 20:41:35 history
[Root @ localhost ~] #! 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 20:30:59 cd
5 20:30:59 hostory
6 20:30:59 history
7 20:30:59 vi ~ /. Bash_profile
8 2016-07-29 20:30:59 history
9 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 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 20:40:32 ls
16 20:40:33 cd
17 2016-07-29 20:40:45 ls/etc/passwd
18 20:41:35 history
19 2016-07-29 20:47:03 history
20 20:48:22 history
If you are interested, you can experiment one by one. This article introduces you here.
Common shortcut keys
Call the last parameter in the previous command again:
! $
Esc,. (click Esc and release, and then click)
These two are very common, especially Esc ,.
After creating a file, we usually modify it or perform other read operations. At this time, you can quickly complete the required commands by typing the command and using the preceding shortcut key.
This article permanently updates the link address: