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