.bash_history:它总是记录我发出的每个命令吗?

时间:2011-11-17 14:23:51

标签: linux bash

我希望能够查看我的命令历史记录(一直回到用户的开头)。

是否有保证.bash_history将继续附加?

如果存在文件开始被截断的限制(希望从头开始)有没有办法删除该限制?

5 个答案:

答案 0 :(得分: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作业)到安全位置。

历史记录截断始终首先删除最旧的条目,如上面的联机帮助页摘录中所述。

答案 1 :(得分:2)

man bash是你的朋友:HISTFILESIZE变量:

  

历史记录文件中包含的最大行数。为此变量分配值时,如有必要,将删除历史文件                 最旧的条目,包含不超过该行数。默认值为500.历史文件在写入时也会被截断为此大小                 交互式shell退出。

答案 2 :(得分:2)

如果您希望所有会话中的所有命令立即附加到历史记录中(以便您可以在另一个会话中立即使用另一个会话中的命令),则可以将以下行放入.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

答案 3 :(得分:0)

另请注意。默认情况下,如果您使用多个登录会话(我通常将2或3个putty窗口登录到同一服务器中),则他们不会看到每个人的历史记录。

此外,当我退出所有炮弹时,最后一个炮弹退出的是保存回文件的炮弹(即我第二天开始工作时可见)。所以我假设bash将历史记录保存在内存中,然后在退出时刷新到文件,至少这是我根据我所看到的猜测。

答案 4 :(得分:0)

亚当建议不设置大小限制并不会削减它,但是 你可以避免shell的其他实例改变HISTSIZE,HISTFILESIZE,...... 通过在/ etc / profile中设置readonly标志。

unset HISTSIZE
unset HISTFILESIZE
readonly HISTFILE HISTFILESIZE HISTSIZE HISTCONTROL HISTIGNORE HISTTIMEFORMAT