日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

[Linux]history命令用法详解

發(fā)布時(shí)間:2023/12/16 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Linux]history命令用法详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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查看效果。

[root@centos7 ~]# history -c [root@centos7 ~]# history1 history [root@centos7 ~]#

以上為清除內(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,清理操作我就不再做演示。

[root@centos7 ~]# cat .bash_history history reboot init 3 中間省略 cat .bash_history histroy -a history -a

先查看.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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美高清视频在线观看 | hd极品free性xxx护士 | 狠狠操狠狠操狠狠操 | 久久理论视频 | 午夜精品一二三区 | 午夜视频免费观看 | 日韩欧| 午夜老湿机 | 色哟哟黄色| 法国空姐电影在线观看 | 一区二区三区精 | 最新av网站在线观看 | 豆花视频在线播放 | 国产精品婷婷午夜在线观看 | 91精品国产乱码久久久久 | 欧美韩日一区二区 | 妖精视频一区二区三区 | 午夜精品免费观看 | 国产又粗又硬又黄的视频 | 免费a级 | 国产日韩不卡 | 超碰人人草人人干 | 1区2区3区在线观看 久久久久久久久久久影院 成人网址在线观看 | 精品啪啪 | 伊人影视网 | www.日本黄色| 蜜臀av性久久久久蜜臀av麻豆 | 欧美丰满美乳xxⅹ高潮www | 蜜色影院 | 韩国精品在线观看 | 日韩成人无码影院 | 国产精品久久久久久久免费看 | 国产高潮呻吟久久 | 黄色国产在线观看 | 亚洲AV无码精品黑人黑人 | 蜜臀中文字幕 | 欧美成人精品欧美一 | 性欧美丰满熟妇xxxx性久久久 | 国产jizz18女人高潮 | 空姐吹箫视频大全 | 天天看天天干 | 日日操狠狠操 | 久久久久久影视 | 蜜桃免费av | 久久波多野 | 日本视频在线观看免费 | 日本japanese极品少妇 | 日日好av | 欧亚av| 中文在线亚洲 | 国产精品久久视频 | 成人在线综合网 | 日韩激情网站 | 嫩草av在线 | 激情综合网婷婷 | 无码精品a∨在线观看中文 福利片av | 中文一区二区在线观看 | 国产又粗又深又猛又爽又在线观看 | 欧美成人精品欧美一级乱黄 | www.日韩.com| 亚洲精品乱码久久久久久不卡 | 亚洲精品一二三四 | 日韩欧美一区二区区 | 美女狂揉羞羞的视频 | 99精品热 | 亚洲自拍在线观看 | 一区二区精彩视频 | 深夜免费在线视频 | 日日摸日日添日日躁av | 亚洲色图在线观看视频 | 日韩美女三级 | 日本少妇bb | www色中色 | 国产少女免费观看高清 | 好爽又高潮了毛片 | 亚洲a∨无码无在线观看 | 免费黄色高清视频 | 在线视频 亚洲 | www精品国产 | 亚洲一二三不卡 | 亚州精品毛片 | 免费毛片一区二区三区久久久 | 九一精品视频 | 91av免费在线观看 | 中文字幕人妻一区二区 | 3o一40一50一6o女人毛片 | jzzjzzjzz亚洲成熟少妇 | 狠狠干狠狠艹 | 欧美啪啪一区二区 | 国产一区二区不卡视频 | 99热中文 | 国模小丫大尺度啪啪人体 | 亚洲a一区 | 自拍偷拍日韩精品 | 亚洲天堂性 | 在线播放无码后入内射少妇 | 黄色大片网站在线观看 | 国产字幕侵犯亲女 | 国产又粗又猛又爽又黄的视频一 |