带有日期和时间的Linux命令历史记录

Rus*_*hvi 20 linux history

我想检查我的linux系统何时触发了哪个命令 - 在哪个日期和时间.

我解雇了这样的命令:

history 50 
Run Code Online (Sandbox Code Playgroud)

它显示了最后50个命令的历史记录,但没有显示它被触发的日期和时间.有谁知道怎么做?

Hos*_*ani 21

关于此链接,您可以通过执行以下命令使krzyk永久提供第一个解决方案:

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
source ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)

  • 方的回答有临时解决办法。 (4认同)
  • 它显示了所有以前执行的错误命令的今天的日期和时间,有什么办法可以在执行以前的命令时获得正确的日期和时间? (3认同)
  • 如果您使用的是zsh:`history -E` (2认同)
  • 您将希望在所有交互式 shell 中使用此功能,而不仅仅是登录 shell。因此,您希望在 ~/.bashrc 而不是 ~/.bash_profile 中设置 HISTTIMEFORMAT (2认同)

evo*_*ved 10

如果您使用的是 zsh,您可以使用例如-Eor-i开关:

history -E
Run Code Online (Sandbox Code Playgroud)

如果您执行man zshoptionsman zshbuiltins您可以找到有关这些开关的更多信息以及与历史记录相关的其他信息:

Also when listing,
 -d     prints timestamps for each event
 -f     prints full time-date stamps in the US `MM/DD/YY hh:mm' format
 -E     prints full time-date stamps in the European `dd.mm.yyyy hh:mm' format
 -i     prints full time-date stamps in ISO8601 `yyyy-mm-dd hh:mm' format
 -t fmt prints time and date stamps in the given format; fmt is formatted with the strftime function with the zsh extensions  described  for  the  %D{string} prompt format in the section EXPANSION OF PROMPT SEQUENCES in zshmisc(1).  The resulting formatted string must be no more than 256 characters or will not be printed
 -D     prints elapsed times; may be combined with one of the options above
Run Code Online (Sandbox Code Playgroud)


Fan*_*ang 7

试试这个:

> HISTTIMEFORMAT="%d/%m/%y %T "

> history
Run Code Online (Sandbox Code Playgroud)

当然,您可以根据自己的喜好调整格式.

  • 这不会显示命令被触发时的日期和时间。假设昨天发出了命令,但如果我按照你的建议写,它将显示今天的日期。这是我不想要的。 (5认同)
  • @ RJ07:这对过去执行的命令不起作用,因为没有为它们保存时间戳.但是,如果将其添加到bash.src,则将保存所有未来bash输入的时间戳. (2认同)

Krz*_*soń 6

它取决于标准 bash 中的 shell(及其配置),仅存储命令而不存储日期和时间(检查.bash_history那里是否有时间戳)。

要让 bash 存储您需要在执行命令HISTTIMEFORMAT 之前设置的时间戳,例如 in.bashrc.bash_profile。这将导致 bash 将时间戳存储在.bash_history(请参阅以 开头的条目#)。

  • 确切地说,如果它没有存储在任何地方,您就无法将其取出。 (2认同)

小智 5

HISTTIMEFORMAT="%d/%m/%y %H:%M "
Run Code Online (Sandbox Code Playgroud)

对于在此之前键入的任何命令,这都没有帮助,因为它们只会获得打开历史记录的默认时间,但它将记录此后任何其他命令的时间。

如果您希望它永久记录历史记录,您应该将以下行放入您的~/.bashrc

export HISTTIMEFORMAT="%d/%m/%y %H:%M "
Run Code Online (Sandbox Code Playgroud)