我们执行过的命令 Linux 都会记录,预设可以记录 1000 条历史命令。这些命令保存在用户的家目录的 .bash_history 文件中。但需要注意的是,只有当用户正常退出当前 shell 时,在当前 shell 中运行的命令才会保存至 .bash_history 文件中
查看 history
[root@evan-01 ~]# ls /root/.bash_history
[root@evan-01 ~]# cat /root/.bash_history
...(省略很多内容)
./configure --prefix=/usr/local/apache2.2
echo $?
make
echo $?
make install
echo $?
ls /usr/local/src/
ls /usr/local/apache2.2/
cd /usr/local/apache2.2/
ls
init 0
[root@evan-01 ~]#
查看存了多少条记录
[root@evan-01 ~]# history
...(省略很多内容)
989 ./configure --prefix=/usr/local/apache2.2
990 echo $?
991 make
992 echo $?
993 make install
994 echo $?
995 ls /usr/local/src/
996 ls /usr/local/apache2.2/
997 cd /usr/local/apache2.2/
998 ls
999 init 0
1000 ls /root/.bash_history
1001 cat /root/.bash_history
1002 history
[root@evan-01 ~]#
但是为什么我现在执行 history 后可以出现大于1000多条记录。可以这样理解:
1、如果你不注销或者关机,那么执行hisotry命令,可能记录大于1000,但不会被写入到 .bash_history
2、如果你注销了以后,.bash_history只保存最近的1000条记录
变量 HISTSIZE
[root@evan-01 ~]# echo $HISTSIZE
1000
[root@evan-01 ~]#
修改变量 HISTSIZE 值
[root@evan-01 ~]# vim /etc/profile
按 i 切换编辑模式,/HISTSIZE 搜索到 HISTSIZE=1000,更改 HISTSIZE=1000 为 HISTSIZE=5000。按 esc 键,输入 :wq 保存退出
查看大小
[root@evan-01 ~]# echo $HISTSIZE
1000
[root@evan-01 ~]#
发现还没有更改过来。source 一下
source
source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。
[root@evan-01 ~]# source /etc/profile
[root@evan-01 ~]# echo $HISTSIZE
5000
[root@evan-01 ~]#
让 history 里面的记录显示时间
[root@evan-01 ~]# history
...(省略很多内容)
994 echo $?
995 ls /usr/local/src/
996 ls /usr/local/apache2.2/
997 cd /usr/local/apache2.2/
998 ls
999 init 0
1000 ls /root/.bash_history
1001 cat /root/.bash_history
1002 history
1003 echo $HISTSIZE
1004 vim /etc/profile
1005 echo $HISTSIZE
1006 source /etc/profile
1007 echo $HISTSIZE
1008 history
[root@evan-01 ~]#
现在看,只显示行号和命令,没有具体的时间信息
设置变量
[root@evan-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@evan-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@evan-01 ~]#
再来查看
987 2019/09/12 15:18:52 ./configure --prefix=/usr/local/apache2.2
988 2019/09/12 15:18:52 echo $?
989 2019/09/12 15:18:52 ./configure --prefix=/usr/local/apache2.2
990 2019/09/12 15:18:52 echo $?
991 2019/09/12 15:18:52 make
992 2019/09/12 15:18:52 echo $?
993 2019/09/12 15:18:52 make install
994 2019/09/12 15:18:52 echo $?
995 2019/09/12 15:18:52 ls /usr/local/src/
996 2019/09/12 15:18:52 ls /usr/local/apache2.2/
997 2019/09/12 15:18:52 cd /usr/local/apache2.2/
998 2019/09/12 15:18:52 ls
999 2019/09/12 15:18:52 init 0
1000 2019/09/12 15:19:16 ls /root/.bash_history
1001 2019/09/12 15:19:18 cat /root/.bash_history
1002 2019/09/12 15:20:59 history
1003 2019/09/12 15:23:40 echo $HISTSIZE
1004 2019/09/12 15:24:38 vim /etc/profile
1005 2019/09/12 15:26:18 echo $HISTSIZE
1006 2019/09/12 15:26:47 source /etc/profile
1007 2019/09/12 15:26:48 echo $HISTSIZE
1008 2019/09/12 15:27:47 history
1009 2019/09/12 15:28:36 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
1010 2019/09/12 15:28:43 echo $HISTTIMEFORMAT
1011 2019/09/12 15:29:13 history
[root@evan-01 ~]#
切换个终端,再来 echo 一遍
发现什么都没有
因为 $HISTTIMEFORMAT 只是在当前终端里生效,所以切换个系统后就不生效了。系统默认这个环境变量是不存在的,需要我们去定义它
定义 $HISTTIMEFORMAT 变量
[root@evan-01 ~]# vim /etc/profile
添加以下内容,按 esc 键,输入 :wq 保存退出:
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
source
source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。
[root@evan-01 ~]# source /etc/profile
重新打开个终端,再来 echo 一遍
[root@evan-01 ~]# history
...(省略很多内容)
987 2019/09/12 15:33:53 ./configure --prefix=/usr/local/apache2.2
988 2019/09/12 15:33:53 echo $?
989 2019/09/12 15:33:53 ./configure --prefix=/usr/local/apache2.2
990 2019/09/12 15:33:53 echo $?
991 2019/09/12 15:33:53 make
992 2019/09/12 15:33:53 echo $?
993 2019/09/12 15:33:53 make install
994 2019/09/12 15:33:53 echo $?
995 2019/09/12 15:33:53 ls /usr/local/src/
996 2019/09/12 15:33:53 ls /usr/local/apache2.2/
997 2019/09/12 15:33:53 cd /usr/local/apache2.2/
998 2019/09/12 15:33:53 ls
999 2019/09/12 15:33:53 init 0
1000 2019/09/12 15:33:53 echo $HISTTIMEFORMAT
1001 2019/09/12 15:35:41 history
[root@evan-01 ~]#
永久保存 history 不限制条数
[root@evan-01 ~]# chattr +a ~/.bash_history
[root@evan-01 ~]#
用户运行过的所有命令,都会被记录下来,只能往后追加,不能被删除
执行6条命令,关闭当前终端,在另一个终端里查看下
[root@evan-01 ~]# pwd
/root
[root@evan-01 ~]# pwd
/root
[root@evan-01 ~]# pwd
/root
[root@evan-01 ~]# ls
11.txt anaconda-ks.cfg ojbk.txt test2 test2.txt.bak test3.txt.bak
1.txt anaconda-ks.cfg.bak test test2_heard.txt test3_soft.txt txtdir
[root@evan-01 ~]# ls
11.txt anaconda-ks.cfg ojbk.txt test2 test2.txt.bak test3.txt.bak
1.txt anaconda-ks.cfg.bak test test2_heard.txt test3_soft.txt txtdir
[root@evan-01 ~]# ls
11.txt anaconda-ks.cfg ojbk.txt test2 test2.txt.bak test3.txt.bak
1.txt anaconda-ks.cfg.bak test test2_heard.txt test3_soft.txt txtdir
[root@evan-01 ~]#
[root@evan-01 ~]# cat /root/.bash_history
因为我们不是正常退出,直接关闭的终端。所以我们发现记录的不全
一定要正常退出,否则保存的记录就是不完整的,或者不全的
!! 连续 2 个 !表示执行上一条命令
[root@evan-01 ~]# ls
11.txt anaconda-ks.cfg ojbk.txt test2 test2.txt.bak test3.txt.bak
1.txt anaconda-ks.cfg.bak test test2_heard.txt test3_soft.txt txtdir
[root@evan-01 ~]# cat 11.txt
12 packets transmitted, 10 received, 0aaaaah,ha% packet loss, time 8999ms
[root@evan-01 ~]# !!
cat 11.txt
12 packets transmitted, 10 received, 0aaaaah,ha% packet loss, time 8999ms
[root@evan-01 ~]#
!n 这里的 n 是数字,表示执行命令历史中的第 n 条命令
[root@evan-01 ~]# history
...(省略很多内容)
999 2019/09/12 15:33:53 init 0
1000 2019/09/12 15:33:53 echo $HISTTIMEFORMAT
1001 2019/09/12 15:35:41 history
1002 2019/09/12 15:40:17 cat /root/.bash_history
1003 2019/09/12 15:41:36 ls
1004 2019/09/12 15:41:38 cat 11.txt
1005 2019/09/12 15:42:26 history
[root@evan-01 ~]# !1000
echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@evan-01 ~]#
!字符串 执行命令历史中最近的一次 以字符串开头的命令
[root@evan-01 ~]# history
...(省略很多内容)
999 2019/09/12 15:33:53 init 0
1000 2019/09/12 15:33:53 echo $HISTTIMEFORMAT
1001 2019/09/12 15:35:41 history
1002 2019/09/12 15:40:17 cat /root/.bash_history
1003 2019/09/12 15:41:36 ls
1004 2019/09/12 15:41:38 cat 11.txt
1005 2019/09/12 15:42:26 history
1006 2019/09/12 15:43:17 echo $HISTTIMEFORMAT
1007 2019/09/12 15:44:40 history
[root@evan-01 ~]# !cat
cat 11.txt
12 packets transmitted, 10 received, 0aaaaah,ha% packet loss, time 8999ms
[root@evan-01 ~]#