2011-11-17 90 views
5

我希望能夠查看我的命令歷史記錄(一直回到用戶的開頭)。.bash_history:它是否總是記錄我發出的每個命令?

是否有任何保證.bash_history將被繼續添加到?

如果文件開始被截斷(希望從頭開始)有限制,是否有辦法消除這個限制?

+5

這個問題更適合[unix.stackexchange.com(http://unix.stackexchange.com/) –

回答

11

有許多環境變量可以控制歷史在bash中的工作方式。從bash的手冊頁有關摘錄如下:

HISTCONTROL 
      A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which 
      begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be 
      saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current 
      line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does 
      not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and 
      subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL. 
    HISTFILE 
      The name of the file in which command history is saved (see HISTORY below). The default value is ~/.bash_history. If unset, the command history 
      is not saved when an interactive shell exits. 
    HISTFILESIZE 
      The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, by 
      removing the oldest entries, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this 
      size after writing it when an interactive shell exits. 
    HISTIGNORE 
      A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored at the begin- 
      ning of the line and must match the complete line (no implicit `*' is appended). Each pattern is tested against the line after the checks speci- 
      fied by HISTCONTROL are applied. In addition to the normal shell pattern matching characters, `&' matches the previous history line. `&' may be 
      escaped using a backslash; the backslash is removed before attempting a match. The second and subsequent lines of a multi-line compound command 
      are not tested, and are added to the history regardless of the value of HISTIGNORE. 
    HISTSIZE 
      The number of commands to remember in the command history (see HISTORY below). The default value is 500. 

直接回答你的問題:

沒有沒有保證,因爲歷史可以被禁用,一些命令可能不會被存儲(例如開始與一個空白),並且可能會對歷史記錄大小施加限制。

至於歷史大小限制:如果取消設置HISTSIZEHISTFILESIZE

unset HISTSIZE 
unset HISTFILESIZE 

你會防止shell截斷歷史文件。然而,如果你有一個運行shell的實例設置了這兩個變量,它將在退出時截斷你的歷史記錄,所以解決方案非常脆弱。如果您絕對必須保持長期的shell歷史記錄,則不應該依賴shell並定期(例如使用cron作業)將文件複製到安全位置。

歷史截斷總是首先刪除最舊的條目,如上面的manpage摘錄中所述。

2

man bash是你的朋友:在HISTFILESIZE變量:

包含在歷史文件中的行的最大數目。當這個變量被分配一個值時,歷史文件被截斷,如果有必要,通過刪除最舊的條目,包含不超過該行數。默認值爲500.當 交互式shell退出時,歷史文件在寫入後也會截斷爲此大小。

+0

是有一種方法來設置這個,所以沒有限制(如果我輸入這麼多的命令,它會填滿我的硬盤)? –

+0

再想一想,我認爲對我的目的而言,將價值設定爲過高的價值就足夠了。 –

0

另請注意。默認情況下,如果您使用多個登錄會話(通常是2或3個putty窗口登錄到同一臺服務器),他們不會看到其他人的歷史記錄。

另外,當我退出所有的shell時,它的最後一個退出是保存該文件的那個(即當我第二天開始工作時可見)。所以我認爲bash會將歷史記錄保留在內存中,然後在退出時刷新文件,至少這是我根據我所看到的內容猜測的。

+0

是的,我注意到會話單獨保存。有時我發現會話崩潰,並且所有命令都沒有保存!我想避免這種情況。是唯一的方法來設置它,以便它在每個命令上刷新?我希望bash_history文件包含我的命令的同步(按時間順序)副本。我認爲對每一個輸入的命令進行沖洗應該能夠做到。 –

+0

不是我所知道的 – Sodved

+0

,你知道沒有其他方法嗎?我閱讀了一些可以設置的內容,以便在輸入的每個命令中都可以刷新。 –

0

亞當建議解封大小限制不剪,但 你能避免外殼的其他實例改變HISTSIZE,HISTFILESIZE,... 在/ etc/profile文件設置只讀標誌在他們身上。

unset HISTSIZE 
unset HISTFILESIZE 
readonly HISTFILE HISTFILESIZE HISTSIZE HISTCONTROL HISTIGNORE HISTTIMEFORMAT 
1

如果你想從追加到歷史瞬間(這樣就可以從一個會話立即在另一個使用命令)的所有會話的所有命令,你可以把下面的行加入到您。bashrc中:

unset HISTSIZE 
unset HISTFILESIZE 
HISTCONTROL=erasedups 

# append to the history file, don't overwrite it 
shopt -s histappend 
# multi-line commands should be stored as a single command 
shopt -s cmdhist 

# sharing of history between multiple terminals 
# histfile has to be read and saved after each command execution 
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND" 

有關這些命令的詳細信息可以在這裏找到:Preserve bash history in multiple terminal windows

+0

有趣。我得在某個時候嘗試。我已經放棄了bash能夠按照我想要的方式記錄歷史記錄(這實際上是一組明智的行爲)。解決方案是安裝zsh ... –

相關問題