如果你經常使用 Linux 指令行,那麼使用 history(曆史)指令可以有效地提升你的效率。本文将通過執行個體的方式向你介紹 history 指令的 15 個用法。
使用 HISTTIMEFORMAT 顯示時間戳
當你從指令行執行 history 指令後,通常隻會顯示已執行指令的序号和指令本身。如果你想要檢視指令曆史的時間戳,那麼可以執行:
# export HISTTIMEFORMAT='%F %T '
# history | more
1 2008-08-05 19:02:39 service network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-05 19:02:39 cat /etc/redhat-release
注意:這個功能隻能用在當 HISTTIMEFORMAT 這個環境變量被設定之後,之後的那些新執行的 bash 指令才會被打上正确的時間戳。在此之前的所有指令,都将會顯示成設定 HISTTIMEFORMAT 變量的時間。[感謝 NightOwl 讀者補充]
使用 Ctrl+R 搜尋曆史
Ctrl+R 是我經常使用的一個快捷鍵。此快捷鍵讓你對指令曆史進行搜尋,對于想要重複執行某個指令的時候非常有用。當找到指令後,通常再按Enter鍵就可以執行該指令。如果想對找到的指令進行調整後再執行,則可以按一下左或右方向鍵。
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`red‘: cat /etc/redhat-release
[Note: Press enter when you see your command, which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)
快速重複執行上一條指令
有 4 種方法可以重複執行上一條指令:
使用上方向鍵,并回車執行。
按 !! 并回車執行。
輸入 !-1 并回車執行。
按 Ctrl+P 并回車執行。
從指令曆史中執行一個指定的指令
在下面的例子中,如果你想重複執行第 4 條指令,那麼可以執行 !4:
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
通過指定關鍵字來執行以前的指令
在下面的例子,輸入 !ps 并回車,将執行以 ps 打頭的指令:
# !ps
ps aux | grep yp
root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind
root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp
使用 HISTSIZE 控制曆史指令記錄的總行數
将下面兩行内容追加到 .bash_profile 檔案并重新登入 bash shell,指令曆史的記錄數将變成 450 條:
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
使用 HISTFILE 更改曆史檔案名稱
預設情況下,指令曆史存儲在 ~/.bash_history 檔案中。添加下列内容到 .bash_profile 檔案并重新登入 bash shell,将使用 .commandline_warrior 來存儲指令曆史:
HISTFILE=/root/.commandline_warrior
使用 HISTCONTROL 從指令曆史中剔除連續重複的條目
在下面的例子中,pwd 指令被連續執行了三次。執行 history 後你會看到三條重複的條目。要剔除這些重複的條目,你可以将 HISTCONTROL 設定為 ignoredups:
# pwd
# history | tail -4
44 pwd
45 pwd
46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47 history | tail -4
# export HISTCONTROL=ignoredups
# history | tail -3
56 export HISTCONTROL=ignoredups
57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58 history | tail -4
使用 HISTCONTROL 清除整個指令曆史中的重複條目
上例中的 ignoredups 隻能剔除連續的重複條目。要清除整個指令曆史中的重複條目,可以将 HISTCONTROL 設定成 erasedups:
# export HISTCONTROL=erasedups
# service httpd stop
38 pwd
39 service httpd stop
40 history | tail -3
# ls -ltr
# history | tail -6
35 export HISTCONTROL=erasedups
36 pwd
37 history | tail -3
38 ls -ltr
[Note that the previous service httpd stop after pwd got erased]
40 history | tail -6
使用 HISTCONTROL 強制 history 不記住特定的指令
将 HISTCONTROL 設定為 ignorespace,并在不想被記住的指令前面輸入一個空格:
# export HISTCONTROL=ignorespace
# service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]
67 ls -ltr
68 pwd
69 history | tail -3
使用 -c 選項清除所有的指令曆史
如果你想清除所有的指令曆史,可以執行:
# history -c
指令替換
在下面的例子裡,!!:$ 将為目前的指令獲得上一條指令的參數:
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
補充:使用 !$ 可以達到同樣的效果,而且更簡單。[感謝 wanzigunzi 讀者補充]
下例中,!^ 從上一條指令獲得第一項參數:
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
# vi -5 !^
為特定的指令替換指定的參數
在下面的例子,!cp:2 從指令曆史中搜尋以 cp 開頭的指令,并擷取它的第二項參數:
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt
下例裡,!cp:$ 擷取 cp 指令的最後一項參數:
# ls -l !cp:$
使用 HISTSIZE 禁用 history
如果你想禁用 history,可以将 HISTSIZE 設定為 0:
# export HISTSIZE=0
# history
# [Note that history did not display anything]
使用 HISTIGNORE 忽略曆史中的特定指令
下面的例子,将忽略 pwd、ls、ls -ltr 等指令:
# export HISTIGNORE=”pwd:ls:ls -ltr:”
# ls
79 export HISTIGNORE=”pwd:ls:ls -ltr:”
80 service httpd stop
81 history
[Note that history did not record pwd, ls and ls -ltr]
如果你經常使用Linux指令,那麼使用history指令無疑會提升你的工作效率。
History指令主要用于顯示曆史指令記錄内容, 下達曆史紀錄中的指令 。
1>History指令文法:
[test@linux]# history [n]
[test@linux]# history [-c]
[test@linux]# history [-raw] histfiles
參數:
n :數字,要列出最近的 n 筆指令清單
-c :将目前的shell中的所有 history 内容全部消除
-a :将目前新增的history 指令新增入 histfiles 中,若沒有加 histfiles ,
則預設寫入 ~/.bash_history
-r :将 histfiles 的内容讀到目前這個 shell 的 history 記憶中
-w :将目前的 history 記憶内容寫入 histfiles
Linux系統當你在shell(控制台)中輸入并執行指令時,shell會自動把你的指令記錄到曆史清單中,一般儲存在使用者目錄下的.bash_history檔案中。預設儲存1000條,你也可以更改這個值。
如果你鍵入 history, history會向你顯示你所使用的前1000個曆史指令,并且給它們編了号,你會看到一個用數字編号的清單快速從螢幕上卷過。你可能不需要檢視1000個指令中的所有項目, 當然你也可以加入數字來列出最近的 n 筆指令清單。
linux中history指令不僅僅讓我們可以查詢曆史指令而已. 我們還可以利用相關的功能來幫我們執行指令。
2>運作特定的曆史指令
history會列出bash儲存的所有曆史指令,并且給它們編了号,我們可以使用“歎号接編号”的方式運作特定的曆史指令.
文法說明:
[test@linux]# [!number] [!command] [!!]
參數說明:
number :第幾個指令的意思;
command :指令的開頭幾個字母
! :上一個指令的意思!
3>History指令實戰
列出所有的曆史記錄:
[test@linux] # history
隻列出最近10條記錄:
[test@linux] # history 10 (注,history和10中間有空格)
使用指令記錄号碼執行指令,執行曆史清單中的第99條指令
[test@linux] #!99 (!和99中間沒有空格)
重複執行上一個指令
[test@linux] #!!
執行最後一次以rpm開頭的指令(!? ?代表的是字元串,這個String可以随便輸,Shell會從最後一條曆史指令向前搜尋,最先比對的一條指令将會得到執行。)
[test@linux] #!rpm
逐屏列出所有的曆史記錄:
[test@linux]# history | more
立即清空history目前所有曆史指令的記錄
[test@linux] #history -c