天天看點

history

如果你經常使用 Linux 指令行,那麼使用 history(曆史)指令可以有效地提升你的效率。本文将通過執行個體的方式向你介紹 history 指令的15個用法。

1. 使用 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

2. 使用 Ctrl+R 搜尋曆史

Ctrl+R 是我經常使用的一個快捷鍵。此快捷鍵讓你對指令曆史進行搜尋,對于想要重複執行某個指令的時候非常有用。當找到指令後,通常再按Enter鍵就可以執行該指令。如果想對找到的指令進行調整後再執行,則可以按一下左或右方向鍵。

# [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)

3. 快速重複執行上一條指令

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

1. 使用上方向鍵,并回車執行。

2. 按 !! 并回車執行。

3. 輸入 !-1 并回車執行。

4. 按 Ctrl+P 并回車執行。

4. 從指令曆史中執行一個指定的指令

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

1 service network restart

2 exit

3 id

4 cat /etc/redhat-release

# !4

cat /etc/redhat-release

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

在下面的例子,輸入 !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

6. 使用 HISTSIZE 控制曆史指令記錄的總行數

将下面兩行内容追加到 .bash_profile 檔案并重新登入 bash shell,指令曆史的記錄數将變成 450 條:

# vi ~/.bash_profile

HISTSIZE=450

HISTFILESIZE=450

7. 使用 HISTFILE 更改曆史檔案名稱

預設情況下,指令曆史存儲在 ~/.bash_history 檔案中。添加下列内容到 .bash_profile 檔案并重新登入 bash shell,将使用 .commandline_warrior 來存儲指令曆史:

HISTFILE=/root/.commandline_warrior

8. 使用 HISTCONTROL 從指令曆史中剔除連續重複的條目

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

# 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

# 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

9. 使用 HISTCONTROL 清除整個指令曆史中的重複條目

上例中的 ignoredups 隻能剔除連續的重複條目。要清除整個指令曆史中的重複條目,可以将 HISTCONTROL 設定成 erasedups:

# export HISTCONTROL=erasedups

# service httpd stop

38 pwd

39 service httpd stop

40 history | tail -3

# ls -ltr

# history | tail -6

35 export HISTCONTROL=erasedups

36 pwd

37 history | tail -3

38 ls -ltr

[Note that the previous service httpd stop after pwd got erased]

40 history | tail -6

10. 使用 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

11. 使用 -c 選項清除所有的指令曆史

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

# history -c

12. 指令替換

在下面的例子裡,!!:$ 将為目前的指令獲得上一條指令的參數:

# ls anaconda-ks.cfg

anaconda-ks.cfg

# vi !!:$

vi anaconda-ks.cfg

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

# cp anaconda-ks.cfg anaconda-ks.cfg.bak

# vi -5 !^

13. 為特定的指令替換指定的參數

在下面的例子,!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:$

14. 使用 HISTSIZE 禁用 history

如果你想禁用 history,可以将 HISTSIZE 設定為 0:

# export HISTSIZE=0

# history

# [Note that history did not display anything]

15. 使用 HISTIGNORE 忽略曆史中的特定指令

下面的例子,将忽略 pwd、ls、ls -ltr 等指令:

# export HISTIGNORE=”pwd:ls:ls -ltr:”

# ls

79 export HISTIGNORE=”pwd:ls:ls -ltr:”

80 service httpd stop

81 history

[Note that history did not record pwd, ls and ls -ltr]