Linux基礎:history命令
一、為什麽要學習 history 命令 ?
history命令是bash shell 內置命令,history命令有助於我們縮短輸入命令的時間,達到節省命令快捷操作的要求。我們也可以通過查詢history命令,從而審計操作日誌。同時,我們可以隱藏一些含有敏感信息的命令輸入,使系統更加安全。
二、history 命令常見用法 ?
語法:
history [n | -c | -rnaw histfile]
參數:
n:數字,列出最近的 n 條歷史命令
-c:將當前shell 緩存中的 history 內容全部清除
-a:將當前shell緩存中的history 內容append附加到 histfile 中,如果沒有指定 histfile,則默認寫入 ~/.bash_histroy
-r:將 histfile 中的內容讀取到當前shell的緩存中
-w:將當前shell緩存的history歷史列表寫入到指定的文件
-a: 追加本次會話新執行的命令歷史列表至歷史文件,因為多終端所以如果想看當前都發生了什麽操作就可以執行-a進行查看
-n: 讀歷史文件(本地數據)中未讀過的行到歷史列表(內存數據)
-r: 讀歷史文件(本地數據)附加到歷史列表(內存數據)
-w: 保存歷史列表(內存數據)到指定的歷史文件(本地數據)
-s: 展開歷史參數成一行,附加在歷史列表後。用於偽造命令歷史
n:數字,列出【最近】的 n 條歷史命令,默認為10
[[email protected] ~]# history 5 997 ls 998 exit 999 history 1000 exit 1001 history
-c : 清空命令歷史(使用該命令之後,僅對當前shell進程生效, exit 退出後重新進入,原來 .bash_history的內容會重新入讀內存中)
要想徹底清空歷史命令,需要先將 .bash_history的內容刪除,接著使用 history -c, 這樣才會徹底清空命令歷史。
[[email protected] ~]# history ... 996 pstree 997 exit 998 tail -f /var/log/ldap.log 999 ls 1000 exit 1001 history [[email protected]~]# history -c [[email protected] ~]# history 3 history [[email protected] ~]# > .bash_history [[email protected] ~]# exit logout
-d:刪除歷史命令中指定的的記錄; 比如我們想刪除命令歷史中的999行命令記錄,直接使用 history -d 999 即可,然後用 history 查看命令的最後幾行,發現原來999行的歷史命令被下一行的命令所取代
[[email protected] ~]# history 10 997 ls 998 exit 999 history 1000 exit 1001 history 1002 vi .bash_history [[email protected] ~]# history -d 999 [[email protected] ~]# history 10 998 exit 999 exit 1000 history 1001 vi .bash_history 1002 ls 1003 history 1004 history 5 1005 history 10 1006 history -d 999 1007 history 10
-r:讀取歷史文件到歷史列表(將 .bash_history重新讀取一遍,寫入到當前bash進程的內存中)
(如果當前兩個終端同時登錄Linux系統, root(tty1)輸入 n 條命令, root(tty2)也輸入 m 條命令並且將內存中的命令歷史通過 histroy -a 刷新到 .bash_history文件中, 那麽 root(tty1)可以直接執行 history -r ,將root(tty2)寫入到 .bashr_history文件中記錄直接讀取到 當前bash進程的histoty命令歷史內存中)
-a:將bash 內存中歷史命令追加到 .bash_history 歷史命令文件中, 默認只有退出 shell 是才會保存
-w:保存歷史列表到指定的歷史文件(history -w /PATH/TO/SOMEFILE 將內存中命令執行的歷史列表保存到指定的 /PATH/TO/SOMEFILE中)
[[email protected] ~]# history -w /tmp/history.log [[email protected] ~]# cat /tmp/history.log ps -ef ls ls -l cat /etc/issue history -w /tmp/history.log
三、history 工作機制
# !n :調用第n條命令
# !-n:調用倒數第n條命令
# !!:執行上一條命令
# !$:引用前一個命令的最後一個參數同組合鍵Esc,.
# !n:^ 調用第n條命令的第一個參數
# !n:$ 調用第n條命令的最後一個參數
# !m:n 調用第m條命令的第n個參數
# !n:* 調用第n條命令的所有參數
# !string:執行命令歷史中最近一個以指定string開頭的命令
# !string:^ 從命令歷史中搜索以string 開頭的命令,並獲取它的第一個參數
# !string:$ 從命令歷史中搜索以string 開頭的命令,並獲取它的最後一個參數
# !string:n 從命令歷史中搜索以string 開頭的命令,並獲取它的第n個參數
# !string:* 從命令歷史中搜索以string 開頭的命令,並獲取它的所有參數
常用的快捷鍵
執行上一次命令
!!
搜索歷史命令:
Ctrl + R
重新調用前一個命令中最後一個參數:
!$
Esc + .(點擊Esc鍵後松開,然後點擊. 鍵)
這兩個很常用,特別是Esc + .
我們在創建文件後,通常會對其進行修改或其他的讀操作,這時候鍵入命令後利用上述快捷鍵即可快速補全所需命令。
四、history 工作機制
history工作機制:當用戶登錄系統並啟動bash時,會讀取 histfile 變量指定文件中的歷史記錄到當前shell進程的內存中,我們在此bash中的所有操作,也會緩存在內存裏面, 只有在bash退出後,才會把內存中的歷史記錄 flush 到 histfile 變量指定的文件中。
所以會出現這種情況:
我和同事用不同的機器登錄到同一臺linux服務器上, 我用history命令只能查看自己終端所操作的命令,如果我也想看到所有連接到此服務器的終端所操作的命令,應該如何操作呢?
回答是: 看不到, 在用戶退出登錄之前,命令歷史記錄全部在內存中,沒有寫入到文件。
通常 .bash_history 在每個用戶的 $HOME 目錄下,但這是查看上次用戶所操作的歷史記錄。
五、history 相關環境變量
HISTFILESIZE:命令歷史文件記錄的條數
HISTSIZE:命令歷史記錄的條數
[[email protected] ~]# echo $HISTSIZE 1000
HISTFILE:指定歷史文件,默認為每個用戶家目錄下: ~/.bash_history
HISTTIMEFORMAT="%F %T ":顯示時間,可以記錄命令執行的時間
[[email protected] ~]# HISTTIMEFORMAT="%F %T " [[email protected] ~]# history 10 2017-06-23 07:05:15 ps -ef 11 2017-06-23 07:05:16 ls 12 2017-06-23 07:05:18 ls -l 13 2017-06-23 07:05:29 cat /etc/issue
HISTTIMEFORMAT="$(hostname) %F %T ":顯示更加詳細的主機名和命令執行時間
[[email protected] ~]# HISTTIMEFORMAT="$(hostname) %F %T " [[email protected] ~]# history 10 web 2017-06-23 07:05:15 ps -ef 11 web 2017-06-23 07:05:16 ls
HISTTIMEFORMAT="$(tty) %F %T ":顯示用戶登錄終端和命令執行時間
[[email protected] ~]# HISTTIMEFORMAT="$(tty) %F %T " [[email protected] ~]# history 10 /dev/pts/2 2017-06-23 07:05:15 ps -ef 11 /dev/pts/2 2017-06-23 07:05:16 ls
HISTIGNORE="str1:str2:...": 忽略以冒號分隔的 str1, str2 歷史記錄
會忽略接下來輸入凡是有 str1:str2 的命令
控制命令歷史的記錄方式
HISTCONTROL=
ignoredups:忽略重復的命令, “連續且相同”方為【重復】
ignorespace:忽略所有以空白開頭的命令
ignoreboth:ignoredups && ignorespace
erasedups:刪除重復命令,減小 history 的大小
註意:命令直接在命令行進行環境變量的設置,執行時間段僅限於該腳本,設置完變量會立即生效,但是exit退出當前腳本之後,環境變量就會失效;將環境變量寫入配置文件中,/etc/profile(全局變量,對所有用戶有效)或 ~/.bash_profile(單用戶模式,僅對該用戶有效),寫入配置文件中不會立即生效,但是需要重新登錄之後生效。
參考:
http://www.tuicool.com/articles/B7FFVrm
本文出自 “Share your knowledge” 博客,請務必保留此出處http://skypegnu1.blog.51cto.com/8991766/1941153
Linux基礎:history命令