如何在 Zsh shell 中查看历史命令的日期时间戳

Joh*_*ino 67 command-history zsh

当我在 ubuntu 服务器上运行 history 命令时,我得到如下输出:

   history
   ...
   25  cd ~
   26  ls -a
   27  vim /etc/gitconfig
   28  vim ~/.gitconfig
Run Code Online (Sandbox Code Playgroud)

我想查看特定用户的日期时间。但是,当我假设它们时:

su otheruser
export HISTTIMEFORMAT='%F %T  '
history
...
25  cd ~
26  ls -a
27  vim /etc/gitconfig
28  vim ~/.gitconfig
Run Code Online (Sandbox Code Playgroud)

它仍然不显示日期时间。我正在使用 zsh shell。

slm*_*slm 98

我相信 HISTTIMEFORMAT 适用于 Bash shell。如果您正在使用,zsh那么您可以将这些开关用于history命令:

例子

$ history -E
    1   2.12.2013 14:19  history -E
Run Code Online (Sandbox Code Playgroud)

或者\history -E

$ history -i
    1  2013-12-02 14:19  history -E
Run Code Online (Sandbox Code Playgroud)

或者\history -i

$ history -D
    1  0:00  history -E
    2  0:00  history -i
Run Code Online (Sandbox Code Playgroud)

如果您执行 aman zshoptions或 ,man zshbuiltins您可以找到有关这些开关的更多信息以及与history.

摘自 zshbuiltins 手册页

Also when listing,
  -d     prints timestamps for each command
  -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)

调试调用

zsh调用时可以使用以下2种方法进行调试。

方法#1

$ zsh -xv
Run Code Online (Sandbox Code Playgroud)

方法#2

$ zsh
$ setopt XTRACE VERBOSE
Run Code Online (Sandbox Code Playgroud)

无论哪种情况,您都应该在启动时看到如下内容:

$ zsh -xv
#
# /etc/zshenv is sourced on all invocations of the
# shell, unless the -f option is set.  It should
# contain commands to set the command search path,
# plus other important environment variables.
# .zshenv should not contain commands that produce
# output or assume the shell is attached to a tty.
#

#
# /etc/zshrc is sourced in interactive shells.  It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#

## shell functions
...
...
unset -f pathmunge _src_etc_profile_d
+/etc/zshrc:49> unset -f pathmunge _src_etc_profile_d

# Created by newuser for 4.3.10
Run Code Online (Sandbox Code Playgroud)

  • 我必须使用 `\history -E`,我使用 oh-my-zsh (23认同)
  • @JohnMerlino 我想查看带有时间戳的历史记录的服务器上有 zsh 4.3.10 (x86_64-unknown-linux-gnu)。在查看了 `zshbuiltins` 手册页后,我发现我需要使用 `fc`。最终对我有用的是`fc -li`。您也可以将命令编号传递给 `fc`,因此 `fc -li -100` 会列出历史记录中的最后 100 个命令。 (17认同)
  • “未找到事件:-i”“未找到事件:-E”。在运行这些开关之前,我需要在配置文件中加载一些东西吗? (7认同)
  • 这是 oh-my-zsh 的长期(6 年以上)错误,请在 github 上查看此问题:https://github.com/robbyrussell/oh-my-zsh/issues/739 (2认同)

Gab*_*是好人 24

history -Ehistory -i或任何不要为我工作。

zsh --version表明zsh 4.3.6 (x86_64-suse-linux-gnu).

然后fc -li 100工作!它显示了最近的 100 个带有时间戳的命令:)


alp*_*989 8

如果您在 中使用oh-my-zsh插件zshhistory -E或者history -i无法使用(因为它的别名为fc -l 1)。

正如@juanpastas 指出的,尝试

\history -E

或者

\history -i

或者

fc -li 100

  • 我刚刚添加: 'alias History="fc -li 1"' 将历史别名重新定义到我的 ~/.zshrc 文件中,现在它可以按需要工作了! (2认同)