8.2-history命令歷史

我們執行過的命令 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 ~]# 

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章