Linux 中 histroy 很常用, 但是無論對於管理員, 普通使用者還是黑客, 從另一個角度而言, 它對於安全的重要性也不言而喻, 比如説這條命令”mysql –h198.168.0.1 –uroot –p1234”, 很重要的資訊, 資料庫的密碼被破解了. 同樣掌握了 history 命令, 可以使你的效率得到很大提升.
執行 history 命令, 終端會顯示之前執行過的命令及其編號, 而我們執行”! 編號”(例如!10) 或”!ls”(歷史中以 ls 開頭的命令, 以最近執行的優先), 可以執行 history 歷史用相應的指令.
預設情況下, 命令歷史被儲存在.bash_history 檔案中, 可以把下面一行新增到.bash_profile 檔案中, 重新登入 shell, 則.new_command 檔案將取代.bash_history 檔案用來儲存歷史命令.#vi ~/.bash_profile HISTFILE=/root/.new_command.
接下來, 讓我們看看 history 一些小技巧, 也許很有樂趣哦!
1 使用 HISTTIMEFORMAT 在歷史命令中顯示 TIMESTAMP
通常情況下, 我們輸入 history 命令時, 終端中將顯示你剛輸入的命令及其編號. 如果出於審查命令的目的, 和命令一起顯示時間戳, 使用者將很有幫助, 如下所示:
[root@sqj2015 ~]export HISTTIMEFORMAT=”`whoami` %F %T ”  //whoami 用反引號 “
[root@sqj2015 ~]history |more
…..
74  root 2015-11-06 10:05:16 vi ~/.bash_profile
75  root 2015-11-06 10:06:15 vi /root/.bash_profile
76  root 2015-11-06 10:08:58 vi ~/.bash_profile
77  root 2015-11-06 10:09:56 vi /root/.bash_history
78  root 2015-11-06 10:10:16 cd /root
79  root 2015-11-06 10:10:18 ls -a
80  root 2015-11-06 10:35:23 export HISTTIMEFORMAT=”`whoami` %F %T ”
81  root 2015-11-06 10:35:25 history
注: 我們可以使用 history N(N 為數字) 來顯示最近的幾條命令.
 
2 巧妙使用 HISTCONTROL 命令
2.1 使用 HISTCONTROL 消除歷史中連續重複命令
如下, 若連續輸入三次 pwd, 當你使用 history 的時候, 你會看到這三條命令連續出現. 設定 HISTCONTROL 為 ignoredups. 來消除重複命令:
[root@sqj2015 ~]# history 4
108  pwd
109  pwd
110  pwd
111  history
若你設定:export HISTCONTROL=ignoredups, 而你再次連續輸入三次 pwd, 執行 history 就只會顯示一條 pwd 命令了.
 
2.2 使用 HISTCONTROL 消除整個歷史中重複命令
[root@sqj2015 home]# history 4
97  cd /home/
98  pwd
99  history
100  history 4
設定 HISTCONTROL 為 erasedups,export HISTCONTROL=erasedups; 這時候, 當我再次鍵入 pwd 命令, 然後使用 history 會有什麼樣的結果:
[root@sqj2015 home]# history 5
97  cd /home/
98  history
99  history 4
100  pwd
101  history 5
你會發現,cd /home 下的 pwd 命令條目消除了.
 
2.3 使用 HISTCONTROL 強制 history 忽略某條特定命令
設定 HISTCONTROL 為 ignorespace(export HISTCONTROL=ignorespace) 後, 在執行一條命令時, 在該命令前加一個空格來指示 history 忽略該命令. 如下:
[root@sqj2015 home]# export HISTCONTROL=ignorespace
[root@sqj2015 home]#  cd /
[root@sqj2015 /]# history 3
105  history
106  export HISTCONTROL=ignorespace
109  history 3
很明顯 cd / 這條命令在 history 歷史條目中被隱藏了.
 
2.4 檢視/設定 HISTCONTROL 變數值
echo $HISTCONTROL    //檢視 HISTCONTROL 值
export HISTCONTROL=ignoredups/erasedups/ignorespace/  //設定 HISTCONTROL 值
若設定 export HISTCONTROL=  , 為空, 則會取消所有設定.
 
3 使用 c 選項清除所有歷史命令
有時候你或許想要清楚之前所有的歷史命令, 而你又不想讓 history 繼續工作. 就可以執行 history -c 命令.
 
4 替換命令歷史中的內容
當你搜尋歷史命令時, 你可能希望執行一個與剛剛查詢到的歷史命令具有相同的引數的命令, 例如:  “!!:$” 可以將前一個命令引數作為當前命令引數;“!^” 把上一個命令的第一個引數傳遞給當前命令;“!$” 把上一個命令的最後一個引數傳遞給當前命令.
[root@sqj2015 /]# touch 01.txt 02.txt 03.txt
[root@sqj2015 /]# ls 01.txt
01.txt
[root@sqj2015 /]# vi !!:$
vi 01.txt
 
[root@sqj2015 /]# ls 01.txt 02.txt 03.txt
01.txt  02.txt  03.txt
[root@sqj2015 /]# vi !^
vi 01.txt
 
[root@sqj2015 /]# ls 01.txt 02.txt 03.txt
01.txt  02.txt  03.txt
[root@sqj2015 /]# vi !$
vi 03.txt
 
5 替換特定命令的特定引數
[root@sqj2015 /]# ls 01.txt 02.txt 03.txt
01.txt  02.txt  03.txt
[root@sqj2015 /]# vi !ls:2
vi 02.txt
[root@sqj2015 /]# vi !ls:3
vi 03.txt
同樣可以使用 “!ls:^”“!ls:$” 來指定第一個或最後一個引數.
 
6 使用 HISTSIZE 禁用 history
若你想禁用 history, 又不讓 bashshell 記錄你的命令, 就可以把 HISTSIZE 設為 0;
export HISTSIZE=0, 這樣執行 history 後就不會顯示任何資訊了.
 
7 使用 HISTIGNORE 讓 history 在儲存時忽略某些指令
有時候你不想在記錄中看到諸如 pwd ls 之類的基本命令, 可以用 HISIGNORE 忽略這些指令. 但要注意: 在 HISTIGNORE 中新增 ls 只忽略 ls, 不忽略 ls -l , 所以一定要寫出要忽略的指令.
[root@sqj2015 /]# export HISTIGNORE=”pwd:ls:”
[root@sqj2015 /]# pwd
/
[root@sqj2015 /]# ls
[root@sqj2015 /]# ls -l
[root@sqj2015 /]# history 5
13  vi 01.txt
14  vi 03.txt
15  export HISTIGNORE=”pwd:ls:”
16  ls -l
17  history 5
相關博客網站:http://blog.sina.com.cn/s/blog_ecd48c580102vxjn.html