[Linux]history命令用法详解
history命令
簡介
基本
在Linux中,history命令是用于顯示歷史執行命令以及讀取命令歷史文件中的歷史執行的命令到內存中,或者從內存中把執行命令的歷史寫入到保存歷史執行命令的文件中的內部命令。
語法:history (選項) (參數)
選項:-c: 清空命令歷史
-d n: 刪除歷史中指定的第n個命令
n: 顯示最近的n條歷史
-a: 追加本次會話新執行的命令歷史列表至歷史文件
-n: 讀歷史文件中未讀過的行到歷史列表
-r: 讀歷史文件附加到歷史列表
-w: 保存歷史列表到指定的歷史文件
-p: 展開歷史參數成多行,但不存在歷史列表中
-s: 展開歷史參數成一行,附加在歷史列表后
命令歷史儲存文件 .bash_history
儲存命令歷史的文件在
~/bash_history
當我們登陸shell的時候,系統會將保存在文件中的命令歷史讀取到內存中,所以我們直接鍵入 history 便可以查詢命令歷史。
[root@centos7 ~]# history 查看內存中命令歷史記錄1 history2 reboot3 init 34 history5 ls6 cd7 history [root@centos7 ~]#直接鍵入history查詢當前內存中已存在的歷史,那么這個時候.bash_history文件中的歷史也是這樣的嗎?
[root@centos7 ~]# cat .bash_history 查看文件中命令歷史記錄 history reboot [root@centos7 ~]#很明顯,在.bash_history中儲存的命令歷史截止到了reboot重啟命令。
當計算機正常執行命令關閉、重啟或者用戶正常退出的時候系統便會將內存中的命令歷史寫入到.bash_history中去,當用戶重新登陸后又會將之前儲存在.bash_history文件中的命令歷史讀取到內存中。
[root@centos7 ~]# exit logout [root@centos7 ~]# cat .bash_history history reboot init 3 history ls cd history cat .bash_history exit重新登陸后便可以發現.bash_history文件中的命令歷史已經更新了,截止到exit
選項
history -c
history -c命令用于清空命令歷史記錄。注意:這里只是清理內存中的歷史記錄在.bash_history中的命令記錄不會被該命令清除,我將在下面做實例演示。
[root@centos7 ~]# history1 reboot2 init 33 history4 ls5 cd6 history7 cat .bash_history8 exit9 cat .bash_history 10 history11 history -d 112 history [root@centos7 ~]#鍵入history后當前內存中的命令歷史如上所示。
[root@centos7 ~]# history -c 清除內存中的命令歷史記錄 [root@centos7 ~]# history 查看內存中的命令歷史記錄1 history [root@centos7 ~]#當鍵入history -c命令后再次鍵入history便可發現內存中的命令歷史已經清空,當前命令歷史只剩下history -c后的一條命令,那么現在.bash_history中的命令歷史還在嗎?
[root@centos7 ~]# cat .bash_history 查看文件中的命令歷史記錄 history reboot init 3 history ls cd history cat .bash_history exit [root@centos7 ~]#由此可見history -c只是清除內存中的命令歷史,.bash_history中的命令歷史不會被清除。
history -d n
history -d n 用于刪除使用history查詢的內存中的命令歷史中指定的第n個命令,也就是說刪除的是內存中的命令歷史,與.bash_history 文件無關。
[root@centos7 ~]# history1 history2 cat .bash_history3 history [root@centos7 ~]# history -d 1 刪除第1條記錄 [root@centos7 ~]# history1 cat .bash_history2 history3 history -d 14 history [root@centos7 ~]#由上面的實例可以看到,我第一次查詢命令歷史第1條命令是history,當執行history -d 1后第1條命令歷史變成了cat .bash_history,在第1次查詢命令歷史的時候該命令是第2條。當history -d n執行成功后被刪除的那條歷史命令就會被后面的歷史所替代,后面的命令歷史都向前靠攏。
history n
history n用于顯示最近n條命令。
實例:
[root@centos7 ~]# history1 cat .bash_history2 history3 history -d 14 history [root@centos7 ~]# history 2 顯示最近兩條記錄4 history5 history 2 [root@centos7 ~]#history -a
history -a用于將內存中的命令歷史記錄追加到.bash_history中而不是覆蓋。
第一步先看看歷史文件中有哪些命令歷史
[root@centos7 ~]# cat .bash_history history reboot init 3 history ls cd history cat .bash_history exit然后執行命令后再次查看
[root@centos7 ~]# history -a [root@centos7 ~]# cat .bash_history history 中間省略 exit cat .bash_history history history -d 1 history history 2 cat .bash_history history -a [root@centos7 ~]#命令生效。注意:這里是追加在后面而不是覆蓋整個文件內容。
history -n
history -n用于將.bash_history中不存在于內存中的命令歷史讀取到內存中去。這里注意是只讀取內存中命令歷史沒有的命令歷史,如果內存中已經存在的相同命令歷史就不會再次讀取。
實例:
因為現在內存中命令歷史比.bash_history中的多,所以我先執行history -c命令刪除內存中的命令然后再執行history -n查看效果。
以上為清除內存中命令歷史的操作
[root@centos7 ~]# history -n 讀取文件中未讀取過的記錄 [root@centos7 ~]# history1 history2 history -n3 init 34 history5 ls中間省略18 cat .bash_history19 history -a20 history [root@centos7 ~]#由上可見history -n命令是直接把.bash_history中的命令歷史讀取到內存中已存在的命令歷史后面,而不是前面。
history -r
history -r 讀歷史文件附加到歷史列表,也就是把.bash_history中的命令歷史記錄直接加到內存中命令歷史記錄后面,與history -n不同的是history -n只讀取沒有讀過的內容。
那么為了實驗我還是要先執行history -c清理一下內存中的歷史記錄然后再執行history -r,清理操作我就不再做演示。
先查看.bash_history文件確認記錄內容
[root@centos7 ~]# history -r 讀取歷史文件到內存中命令歷史記錄 [root@centos7 ~]# history1 history2 history -r3 history4 reboot5 init 3中間省略17 cat .bash_history18 histroy -a19 history -a20 history [root@centos7 ~]#如上所示.bash_history中的記錄已讀取
history -w
history -w用于用內存中的命令歷史記錄覆蓋.bash_history中的記錄,直接覆蓋毫不留情。
[root@centos7 ~]# history -c 清空內存中的記錄 [root@centos7 ~]# history1 history [root@centos7 ~]# history -w 將內存中的記錄覆蓋到文件中 [root@centos7 ~]# cat .bash_history history history -w [root@centos7 ~]#如上所示,內存中的記錄覆蓋到了文件中。
history -p
history -p 可以展開歷史參數成多行,但不存在歷史列表中
[root@centos7 ~]# history -c [root@centos7 ~]# history -p aa bb aa bb [root@centos7 ~]# history1 history [root@centos7 ~]#history -s
history -s這個命令相當于直接在內存記錄中添加一條內容。
實例:
[root@centos7 ~]# history -c [root@centos7 ~]# history -s rm -rf /* Linux上最好玩的命令 [root@centos7 ~]# history1 rm -rf /1 /app /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /path /proc /root /run /sbin /srv /sys /tmp /usr /var 2 history [root@centos7 ~]#如上所示,雖然內存歷史記錄里面有這么一條,但是并沒有執行過。
調用歷史參數
簡介
cmd代表任意命令
cmd !^ : 利用上一個命令的第一個參數做cmd的參數
cmd !$ : 利用上一個命令的最后一個參數做cmd的參數
cmd !* : 利用上一個命令的全部參數做cmd的參數
cmd !:n : 利用上一個命令的第n個參數做cmd的參數
cmd !n:^ 調用第n條命令的第一個參數
cmd !n:$ 調用第n條命令的最后一個參數
cmd !n:m 調用第n條命令的第m個參數
cmd !n:* 調用第n條命令的所有參數
cmd !st:^ 從命令歷史中搜索以 st 開頭的命令 ,并獲取它的第一個參數
cmd !st:$ 從命令歷史中搜索以 st 開頭的命令 ,并獲取它的最后一個參數
cmd !st:n 從命令歷史中搜索以 st 開頭的命令 ,并獲取它的第n個參數
cmd !st:* 從命令歷史中搜索以 st 開頭的命令 ,并獲取它的所有參數
所有實例均省略不必要內容
cmd !^
利用上一個命令的第一個參數做cmd的參數
實例:
[root@centos7 ~]# ll / [root@centos7 ~]# cd !^ cd / [root@centos7 /]#cmd !$
利用上一個命令的最后一個參數做cmd的參數
實例:
[root@centos7 ~]# ls /bin /dev [root@centos7 ~]# cd !$ cd /dev [root@centos7 dev]#cmd !*
利用上一個命令的全部參數做cmd的參數
實例:
[root@centos7 ~]# ls /bin /dev [root@centos7 dev]# ll !* ll /bin /dev [root@centos7 dev]#cmd !:n
利用上一個命令的第n個參數做cmd的參數
實例:
[root@centos7 dev]# history1 cd2 cd /boot3 cd ~4 ll ~ [root@centos7 dev]# ls !2 ls cd /boot [root@centos7 dev]#cmd !n:^
調用第n條命令的第一個參數
[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev [root@centos7 ~]# cd !13:^ cd /bin [root@centos7 bin]#cmd !n:$
調用第n條命令的最后一個參數
[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev [root@centos7 ~]# cd !13:$ cd /dev [root@centos7 dev]#cmd !n:m
調用第n條命令的第m個參數
[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev [root@centos7 ~]# cd !13:2 cd /dev [root@centos7 dev]#cmd !n:*
調用第n條命令的所有參數
[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev [root@centos7 ~]# ll !13:* ll /bin /dev [root@centos7 ~]#cmd !st:^
從命令歷史中搜索以 st 開頭的命令 ,并獲取它的第一個參數
[root@centos7 app]# touch a b 在/app下創建 a b兩個文件 [root@centos7 app]# history26 cd /app27 touch a b28 history [root@centos7 app]# ll !tou:^ ll a [root@centos7 app]#cmd !st:$
從命令歷史中搜索以 st 開頭的命令 ,并獲取它的最后1個參數
[root@centos7 app]# touch a b [root@centos7 app]# history26 cd /app27 touch a b28 history [root@centos7 app]# ll !tou:$ ll b [root@centos7 app]#cmd !st:n
從命令歷史中搜索以 st 開頭的命令 ,并獲取它的第n個參數
[root@centos7 app]# touch a b [root@centos7 app]# history26 cd /app27 touch a b28 history [root@centos7 app]# ll !tou:2 ll b [root@centos7 app]#cmd !st:*
從命令歷史中搜索以 st 開頭的命令 ,并獲取它的所有參數
[root@centos7 app]# touch a b [root@centos7 app]# history26 cd /app27 touch a b28 history [root@centos7 app]# ll !tou:* ll a b [root@centos7 app]#命令歷史相關環境變量
HISTSIZE:命令歷史記錄的條數,默認1000 HISTFILE:指定歷史文件,默認為~/.bash_history
HISTFILESIZE:命令歷史文件記錄歷史的條數
HISTIGNORE=“str1:str2:… “ 忽略string1,string2歷史 控制命令歷史的記錄方式:
環境變量:HISTCONTROL
ignoredups 默認,忽略重復的命令,連續且相同為“重復“
ignorespace忽略所有以空白開頭的命令
ignoreboth 相當于ignoredups, ignorespace的組合
export 變量名=”值“ 存放在 /etc/profile 或 ~/.bash_profile
總結
以上是生活随笔為你收集整理的[Linux]history命令用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科学计数法(PAT)
- 下一篇: linux history命令详解