歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下history命令詳解(2)

Linux下history命令詳解(2)

日期:2017/2/25 10:38:43   编辑:Linux教程
 使用 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

  命令替換

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

  # ls anaconda-ks.cfg

  anaconda-ks.cfg

  # vi !!:$

  vi anaconda-ks.cfg

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

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

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

  anaconda-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:2

  ls -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 -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]

  bash的設置

  運行 set|grep HISTFILE,默認的歷史操作記錄文件是 .bash_history

  在.bash_profile 添加

  HISTFILE=/root/test

  export HISTFILE

  重新登錄,會發現已經把記錄寫道/root/test 了。

  其他設置都寫在.bashrc可實現:

  # 忽略重復的命令

  export HISTCONTROL=ignoredups

  # 忽略由冒號分割的這些命令

  export HISTIGNORE="[ ]*:&:bg:fg:exit"

  # 設置保存歷史命令的文件大小

  export HISTFILESIZE=1000000000

  # 保存歷史命令條數

  export HISTSIZE=1000000

  由於bash的history文件默認是覆蓋,如果存在多個終端,最後退出的會覆蓋以前歷史記錄,改為追加形式:

  shopt -s histappend

  實時寫入,而不是退出shell才寫入的方法:

  PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Copyright © Linux教程網 All Rights Reserved