1. 程式人生 > >linux中Shell歷史命令記錄檔案的路徑是什麼

linux中Shell歷史命令記錄檔案的路徑是什麼

如果你經常使用 Linux 命令列,那麼使用 history(歷史)命令可以有效地提升你的效率。本文將通過例項的方式向你介紹 history 命令的 15 個用法。

使用 HISTTIMEFORMAT 顯示時間戳

當你從命令列執行 history 命令後,通常只會顯示已執行命令的序號和命令本身。如果你想要檢視命令歷史的時間戳,那麼可以執行:

# export HISTTIMEFORMAT='%F %T '# history | more1  2008-08-05 19:02:39 service network restart2  2008-08-05 19:02:39 exit3  2008-08-05 19:02:39 id4
  2008-08-05 19:02:39 cat /etc/redhat-release

注意:這個功能只能用在當 HISTTIMEFORMAT 這個環境變數被設定之後,之後的那些新執行的 bash 命令才會被打上正確的時間戳。在此之前的所有命令,都將會顯示成設定 HISTTIMEFORMAT 變數的時間。[感謝 NightOwl 讀者補充]

使用 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-releaseFedora release 9 (Sulphur)

快速重複執行上一條命令

有 4 種方法可以重複執行上一條命令:

  1. 使用上方向鍵,並回車執行。
  2. 按 !! 並回車執行。
  3. 輸入 !-1 並回車執行。
  4. 按 Ctrl+P 並回車執行。

從命令歷史中執行一個指定的命令

在下面的例子中,如果你想重複執行第 4 條命令,那麼可以執行 !4:

# history | more1  service network restart2  exit3  id4  cat /etc/redhat-release# !4cat /etc/redhat-releaseFedora release 9 (Sulphur)

通過指定關鍵字來執行以前的命令

在下面的例子,輸入 !ps 並回車,將執行以 ps 打頭的命令:

# !psps aux | grep yproot     16947  0.0  0.1  36516  1264 ?        Sl   13:10   0:00 ypbindroot     17503  0.0  0.0   4124   740 pts/0    S+   19:19   0:00 grep yp

使用 HISTSIZE 控制歷史命令記錄的總行數

將下面兩行內容追加到 .bash_profile 檔案並重新登入 bash shell,命令歷史的記錄數將變成 450 條:

# vi ~/.bash_profileHISTSIZE=450HISTFILESIZE=450

使用 HISTFILE 更改歷史檔名稱

預設情況下,命令歷史儲存在 ~/.bashhistory 檔案中。新增下列內容到 .bashprofile 檔案並重新登入 bash shell,將使用 .commandline_warrior 來儲存命令歷史:

# vi ~/.bash_profileHISTFILE=/root/.commandline_warrior

使用 HISTCONTROL 從命令歷史中剔除連續重複的條目

在下面的例子中,pwd 命令被連續執行了三次。執行 history 後你會看到三條重複的條目。要剔除這些重複的條目,你可以將 HISTCONTROL 設定為 ignoredups:

# pwd# pwd# pwd# history | tail -444  pwd45  pwd46  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 -356  export HISTCONTROL=ignoredups57  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 -338  pwd39  service httpd stop40  history | tail -3# ls -ltr# service httpd stop# history | tail -635  export HISTCONTROL=erasedups36  pwd37  history | tail -338  ls -ltr39  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 -367  ls -ltr68  pwd69  history | tail -3

使用 -c 選項清除所有的命令歷史

如果你想清除所有的命令歷史,可以執行:

# history -c

命令替換

在下面的例子裡,!!:$ 將為當前的命令獲得上一條命令的引數:

# ls anaconda-ks.cfganaconda-ks.cfg# vi !!:$vi anaconda-ks.cfg

補充:使用 !$ 可以達到同樣的效果,而且更簡單。[感謝 wanzigunzi 讀者補充]

下例中,!^ 從上一條命令獲得第一項引數:

# cp anaconda-ks.cfg anaconda-ks.cfg.bakanaconda-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:2ls -l /really/a/very/long/path/long-filename.txt

下例裡,!cp:$ 獲取 cp 命令的最後一項引數:

# ls -l !cp:$ls -l /really/a/very/long/path/long-filename.txt

使用 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 -379  export HISTIGNORE=”pwd:ls:ls -ltr:”80  service httpd stop81  history[Note that history did not record pwd, ls and ls -ltr]