【问题标题】:Linux Command History with date and time带有日期和时间的 Linux 命令历史记录
【发布时间】:2016-11-26 08:46:47
【问题描述】:

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

我发出了这样的命令:

history 50 

它显示了最近 50 条命令的历史记录,但没有显示它被触发的日期和时间。有人知道怎么做吗?

【问题讨论】:

标签: linux history


【解决方案1】:

试试这个:

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

> history

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

【讨论】:

  • 当它被触发时,它不显示命令的日期和时间。假设命令昨天被解雇,但如果我按照你的建议写,它将显示今天的日期。这是我不想要的。
  • @RJ07:这不适用于过去执行的命令,因为没有为它们保存时间戳。但是,如果您将其添加到您的 bash.src 中,则会保存时间戳以供将来所有 bash 输入使用。
  • 好的,这只适用于未来。正如 Rushvi 所说,它不会更新过去的历史……不过还是可以的。
【解决方案2】:

这取决于标准 bash 中的 shell(及其配置),仅存储命令而不包含日期和时间(如果有任何时间戳,请检查 .bash_history)。

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

【讨论】:

  • 那么如果存储没有时间和时间那么现在就没有办法获取时间了吗?
  • 没错,如果它没有存储在任何地方,你就无法将其取出。
  • 哦。感谢回复
【解决方案3】:

Regarding this link 你可以通过执行使first solution provided by krzyk 永久化:

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
source ~/.bash_profile

【讨论】:

  • 方的回答有临时解决办法。
  • 它显示了今天所有以前执行的命令的日期和时间,这是错误的,有什么办法可以让我在以前的命令执行时获得正确的日期和时间?
  • 我猜,不是因为文件 bash 历史只是一个没有更多数据的文本。
  • 如果你使用 zsh:history -E
  • 您将希望在所有交互式 shell 中都使用此功能,而不仅仅是登录 shell。因此,您希望在 ~/.bashrc 而不是 ~/.bash_profile 中设置 HISTTIMEFORMAT
【解决方案4】:
HISTTIMEFORMAT="%d/%m/%y %H:%M "

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

如果您希望它永久记录历史记录,您应该输入以下内容 在你的~/.bashrc 中加入

export HISTTIMEFORMAT="%d/%m/%y %H:%M "

【讨论】:

    【解决方案5】:

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

    history -E
    

    如果您执行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
    

    【讨论】:

    • 这个答案最合适。我有zsh,所以在.zshrc 中显式设置格式似乎是多余的,如果我们可以使用预定义的选项做同样的事情。
    猜你喜欢
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2012-05-26
    相关资源
    最近更新 更多