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

带有日期和时间的Linux命令历史记录,linux,history,Linux,History,我想在我的linux系统上检查什么时候启动了哪个命令——在什么日期和时间 我发出了如下命令: history 50 它向我显示了最近50条命令的历史记录,但没有显示它被触发的日期和时间。有人知道怎么做吗?试试这个: > HISTTIMEFORMAT="%d/%m/%y %T " > history 当然,您可以根据自己的喜好调整格式 这取决于标准bash中的shell(及其配置),仅存储命令而不包含日期和时间(如果有时间戳,请检查.bash\u history) 要让bash

我想在我的linux系统上检查什么时候启动了哪个命令——在什么日期和时间

我发出了如下命令:

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

试试这个:

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

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

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

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

您可以通过执行以下操作来创建永久性:

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
source ~/.bash_profile
对于在此之前键入的任何命令,它都没有帮助,因为它们只会获得默认时间,即您打开历史记录的时间,但它会记录在此之后任何其他命令的时间

如果您想让它记录永久的历史记录,您应该放置以下内容 您的
~/.bashrc

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

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

history -E
如果您执行
man zshoptions
man 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

即使在这里:设置
HISTTIMEFORMAT
也会更改未来命令的格式,以便为它们保存时间戳。设置
HISTTIMEFORMAT
之前激发的命令的时间戳未保存在histfile中,因此无法显示它们。如果使用zsh:
history-E
这不会显示命令激发时的日期和时间。假设命令昨天被触发,但若我按照你们的建议写,它将显示今天的日期。我不想要它。@RJ07:这对过去执行的命令不起作用,因为没有为它们保存时间戳。但是,如果将其添加到bash.src中,则时间戳将被保存以供将来所有bash输入使用。好的,这只适用于将来。正如Rushvi所说,它不会更新过去的历史。。。但是仍然可以。如果它没有时间存储,那么现在就没有办法获取时间了?没错,如果它没有存储在任何地方,你就无法获取它。ohk。谢谢你的回答。英芳的回答有一个临时解决方案。它显示了所有以前执行的命令的今天的日期和时间。这是错误的。有没有办法让我知道以前执行命令的正确日期和时间?我猜,这不是因为文件bash history只是一个文本,没有任何更多的数据。如果您使用zsh:
history-E
,您将希望在所有交互式shell中使用它,而不仅仅是登录shell。因此,您希望在~/.bashrc而不是~/.bash_配置文件中设置HISTTIMEFORMAT。这个答案最适合。我有
zsh
,所以如果我们可以用预定义的选项做同样的事情,那么在
.zshrc
中显式设置格式似乎是多余的。
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