若是你常用 Linux 命令行,那麼使用 history(歷史)命令能夠有效地提高你的效率。本文將經過實例的方式向你介紹 history 命令的 15 個用法。html
當你從命令行執行 history 命令後,一般只會顯示已執行命令的序號和命令自己。若是你想要查看命令歷史的時間戳,那麼能夠執行:
linux
# 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 讀者補充]shell
Ctrl+R 是我常用的一個快捷鍵。此快捷鍵讓你對命令歷史進行搜索,對於想要重複執行某個命令的時候很是有用。當找到命令後,一般再按回車鍵就能夠執行該命令。若是想對找到的命令進行調整後再執行,則能夠按一下左或右方向鍵。
安全
# [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 種方法能夠重複執行上一條命令:bash
在下面的例子中,若是你想重複執行第 4 條命令,那麼能夠執行 !4:
this
# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)
在下面的例子,輸入 !ps 並回車,將執行以 ps 打頭的命令:
spa
# !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
將下面兩行內容追加到 .bash_profile 文件並從新登陸 bash shell,命令歷史的記錄數將變成 450 條:
命令行
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
默認狀況下,命令歷史存儲在 ~/.bash_history 文件中。添加下列內容到 .bash_profile 文件並從新登陸 bash shell,將使用 .commandline_warrior 來存儲命令歷史:
rest
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
在下面的例子中,pwd 命令被連續執行了三次。執行 history 後你會看到三條重複的條目。要剔除這些重複的條目,你能夠將 HISTCONTROL 設置爲 ignoredups:
code
# pwd
# pwd
# 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
# pwd
# pwd
# pwd
# 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
上例中的 ignoredups 只能剔除連續的重複條目。要清除整個命令歷史中的重複條目,能夠將 HISTCONTROL 設置成 erasedups:
# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38 pwd
39 service httpd stop
40 history | tail -3
# ls -ltr
# service httpd stop
# history | tail -6
35 export HISTCONTROL=erasedups
36 pwd
37 history | tail -3
38 ls -ltr
39 service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40 history | tail -6
將 HISTCONTROL 設置爲 ignorespace,並在不想被記住的命令前面輸入一個空格:
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3
若是你想清除全部的命令歷史,能夠執行:
# history -c
在下面的例子裏,!!:$ 將爲當前的命令得到上一條命令的參數:
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
補充:使用 !$ 能夠達到一樣的效果,並且更簡單。[感謝 wanzigunzi 讀者補充]
下例中,!^ 從上一條命令得到第一項參數:
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg
在下面的例子,!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:$
ls -l /really/a/very/long/path/long-filename.txt
若是你想禁用 history,能夠將 HISTSIZE 設置爲 0:
# export HISTSIZE=0
# history
# [Note that history did not display anything]
下面的例子,將忽略 pwd、ls、ls -ltr 等命令:
# export HISTIGNORE=」pwd:ls:ls -ltr:」
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
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
除了使用history命令,在 shell 或 GUI 終端提示下,你也可使用上下方向鍵來翻閱命令歷史(向下箭頭會向前翻閱),直到你找到所需命令爲止。這可讓咱們很方便地編輯前面的某一條命令,而不用重複輸入相似的命令。
History命令的用途確實很大!但須要當心安全的問題!尤爲是 root 的歷史紀錄檔案,這是黑客們的最愛!由於不當心的 root 會將不少的重要資料在執行的過程當中會被紀錄在 ~/.bash_history 當中,若是這個檔案被解析的話,後果不堪設想!
來源:http://blog.sina.com.cn/s/blog_5caa94a00100gyls.html