HISTORY的使用方法總結

來源:互聯網
上載者:User

HISTORY的使用方法總結 匯總了一下HISTORY的使用方法。以後用的時候過來查  :) 一、使用 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二、使用 Ctrl+R 搜尋曆史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 種方法可以重複執行上一條命令:使用上方向鍵,並斷行符號執行。按 !! 並斷行符號執行。輸入 !-1 並斷行符號執行。按 Ctrl+P 並斷行符號執行。四、從命令曆史中執行一個指定的命令在下面的例子中,如果你想重複執行第 4 條命令,那麼可以執行 !4: # 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 打頭的命令: # !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 來儲存命令曆史: # vi ~/.bash_profile HISTFILE=/root/.commandline_warrior八、使用 HISTCONTROL 從命令曆史中剔除連續重複的條目在下面的例子中,pwd 命令被連續執行了三次。執行 history 後你會看到三條重複的條目。要剔除這些重複的條目,你可以將 HISTCONTROL 設定為 ignoredups: # 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九、使用 HISTCONTROL 清除整個命令曆史中的重複條目上例中的 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 強制 history 不記住特定的命令將 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使用 -c 選項清除所有的命令曆史十一、如果你想清除所有的命令曆史,可以執行:# history -c 十二、使用 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:” # 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]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.