敲过的命令,Linux是会有记录的,预设能够记录1000条历史命令。这些命令保存在用户的家目录中的.bash_history
文件中。只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至.bash_history
文件中。css
[root@centos-01 ~]# ls /root/.bash_history
/root/.bash_history
[root@centos-01 ~]# cat !$
history命令若是未改动过环境变量,默承认以把最近1000条历史命令打印出来。linux
[root@centos-01 ~]# history
[root@centos-01 ~]# echo $HISTSIZE
1000
能够在/etc/profile
文件中修改环境变量HISTSIZEshell
[root@centos-01 ~]# vi /etc/profile
若是修改了环境变量HISTSIZE,并想使之生效,那么须要从新进入终端。或者执行命令source /etc/profile
使之生效。vim
[root@centos-01 ~]# source /etc/profile
[root@centos-01 ~]# history -c
[root@centos-01 ~]# history
1 history
[root@centos-01 ~]# cat .bash_history
能够看到,文件中的历史命令并无清空centos
[root@centos-01 ~]# history
1 history
2 cat .bash_history
3 vi /etc/profile
4 source /etc/profile
5 source /etc/profile
6 $HISTSIZE
7 echo $HISTSIZE
8 history
[root@centos-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@centos-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@centos-01 ~]# history
1 2018/05/06 00:11:46 history
2 2018/05/06 00:12:57 cat .bash_history
3 2018/05/06 00:16:12 vi /etc/profile
4 2018/05/06 00:18:16 source /etc/profile
5 2018/05/06 00:18:34 source /etc/profile
6 2018/05/06 00:19:41 $HISTSIZE
7 2018/05/06 00:19:57 echo $HISTSIZE
8 2018/05/06 00:20:27 history
9 2018/05/06 00:24:36 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
10 2018/05/06 00:24:54 echo $HISTTIMEFORMAT
11 2018/05/06 00:28:57 history
在新打开的终端中,再次查看环境变量HISTTIMEFORMATruby
[root@centos-01 ~]# echo $HISTTIMEFORMAT
[root@centos-01 ~]#
发现以前修改环境变量HISTTIMEFORMAT,只对当前终端有效,对其余终端无效。bash
要想使设置的环境变量HISTTIMEFORMAT有效,能够编辑/etc/profile
文件。markdown
[root@centos-01 ~]# vim /etc/profile
能够在HISTSIZE=5000
下面另起一行,添加spa
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
保存退出code
[root@centos-01 ~]# source !$
source /etc/profile
再新打开一个终端,查看环境变量HISTTIMEFORMAT
[root@centos-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
chattr +a ~/.bash_history
!!
”连续两个‘!’,表示执行上一条指令[root@centos-01 ~]# ls
anaconda-ks.cfg
[root@centos-01 ~]# !!
ls
anaconda-ks.cfg
“!n”(n是数字),表示执行历史命令中第n条指令。例如“!1002
”,表示执行历史命令中第1002条命令。
“!字符串”(字符串大于等于1),倒着找历史命令中第一个以该字符串开头的命令。例如“!echo
”,表示执行历史命令中最近一次以“echo”开头的指令。