8.2-history命令历史

本文介绍了Linux系统中history命令的使用,包括查看历史命令记录、调整HISTSIZE变量以改变记录数量,以及如何显示时间戳。同时,讨论了$HISTTIMEFORMAT环境变量的设置及其生效范围,强调了正常退出shell以完整保存命令历史的重要性。还提到了!!和!n快捷方式来重复执行历史命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们执行过的命令 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 ~]# 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值