在 Linux 中的 Zsh 中启用历史命令的时间戳

在历史命令输出中启用时间戳有助于我们找到某个命令在 Linux 中执行的时间。 我们已经看到了如何 在 Bash 历史记录中启用时间戳. 今天让我们看看如何启用时间戳 history Linux 中 Zsh shell 中的命令。

在 Zsh Shell 中为历史命令启用时间戳

您可以在 Linux 的 Bash 历史输出中显示日期和时间戳,方法是使用 HISTTIMEFORMAT 环境变量。 但是,您不需要在 Zsh 中设置任何环境变量。 默认情况下,Zsh 有一些内置标志来显示历史命令输出中的日期和时间。

首先,让我们查看之前执行的命令列表 zsh 会话使用 history 命令:

% history

样本输出:

    1  lsb_release -a
    2  uname -r
    3  hostname
    4  ls -l
    5  touch ostechnix.txt
    6  clear
zsh 会话中的历史命令输出

如您所见, history 命令不显示时间戳。 它只显示每个命令的前缀号。

要为历史命令启用时间戳,即在 zsh shell 中的所有命令中显示日期和时间,请使用 -f 标志与 history 命令:

% history -f

现在您将看到日期和时间 history zsh shell 中的命令输出:

    1  11/28/2020 17:00  lsb_release -a
    2  11/28/2020 17:00  uname -r
    3  11/28/2020 17:00  hostname
    4  11/28/2020 17:00  ls -l
    5  11/28/2020 17:00  touch ostechnix.txt
    6  11/28/2020 17:00  clear
    7  11/28/2020 17:01  history
在 Zsh Shell 中为历史命令启用时间戳

在这里, -f 标志用于打印完整的日期和时间戳 'US MM/DD/YY hh:mm' 格式

如果要在 'European dd.mm.yyyy hh:mm' 格式,使用 -E 旗帜。

% history -E

样本输出:

    1  28.11.2020 17:00  lsb_release -a
    2  28.11.2020 17:00  uname -r
    3  28.11.2020 17:00  hostname
    4  28.11.2020 17:00  ls -l
    5  28.11.2020 17:00  touch ostechnix.txt
    6  28.11.2020 17:00  clear
    7  28.11.2020 17:01  history
    8  28.11.2020 17:06  history -f

同样,要打印日期和时间戳 'ISO8601 yyyy-mm-dd hh:mm' 格式,使用 -i 旗帜:

% history -i

样本输出:

    1  2020-11-28 17:00  lsb_release -a
    2  2020-11-28 17:00  uname -r
    3  2020-11-28 17:00  hostname
    4  2020-11-28 17:00  ls -l
    5  2020-11-28 17:00  touch ostechnix.txt
    6  2020-11-28 17:00  clear
    7  2020-11-28 17:01  history
    8  2020-11-28 17:06  history -f
    9  2020-11-28 17:18  history -E

如果只想打印时间,请使用 -d 旗帜。

% history -d

样本输出:

    1  17:00  lsb_release -a
    2  17:00  uname -r
    3  17:00  hostname
    4  17:00  ls -l
    5  17:00  touch ostechnix.txt
    6  17:00  clear
    7  17:01  history
    8  17:06  history -f
    9  17:18  history -E
   10  17:18  history -i

使用在历史输出中显示日期和时间戳 fc 命令

在 zsh shell 中启用历史输出中的时间戳的另一种方法是使用 fc 命令。 这 fc 命令,简称 FCommands 是一个 shell 内置命令,用于在交互式 shell 中列出、编辑和重新执行最近输入的命令。

在历史输出中显示完整的时间戳,使用 fc 命令,只需运行:

% fc -lf

样本输出:

    1  11/28/2020 17:00  lsb_release -a
    2  11/28/2020 17:00  uname -r
    3  11/28/2020 17:00  hostname
    4  11/28/2020 17:00  ls -l
    5  11/28/2020 17:00  touch ostechnix.txt
    6  11/28/2020 17:00  clear
    7  11/28/2020 17:01  history
    8  11/28/2020 17:06  history -f
    9  11/28/2020 17:18  history -E
   10  11/28/2020 17:18  history -i
   11  11/28/2020 17:19  history -d
使用 fc 命令在历史输出中显示日期和时间戳使用 fc 命令在历史输出中显示日期和时间戳

如前所述, -f flag 在 美国格式 IE 'MM/DD/YY hh:mm'.

如果要在历史输出中显示时间戳 欧洲格式 这是 dd.mm.yyyy hh:mm, 采用 -E 旗帜:

% fc -lE

样本输出:

    1  28.11.2020 17:00  lsb_release -a
    2  28.11.2020 17:00  uname -r
    3  28.11.2020 17:00  hostname
    4  28.11.2020 17:00  ls -l
    5  28.11.2020 17:00  touch ostechnix.txt
    6  28.11.2020 17:00  clear
    7  28.11.2020 17:01  history
    8  28.11.2020 17:06  history -f
    9  28.11.2020 17:18  history -E
   10  28.11.2020 17:18  history -i
   11  28.11.2020 17:19  history -d
   12  28.11.2020 17:43  fc -lf

在 in 中显示时间戳 ISO8601 格式 (IE yyyy-mm-dd hh:mm), 采用 -i 旗帜:

% fc -li

样本输出:

    1  2020-11-28 17:00  lsb_release -a
    2  2020-11-28 17:00  uname -r
    3  2020-11-28 17:00  hostname
    4  2020-11-28 17:00  ls -l
    5  2020-11-28 17:00  touch ostechnix.txt
    6  2020-11-28 17:00  clear
    7  2020-11-28 17:01  history
    8  2020-11-28 17:06  history -f
    9  2020-11-28 17:18  history -E
   10  2020-11-28 17:18  history -i
   11  2020-11-28 17:19  history -d
   12  2020-11-28 17:43  fc -lf
   13  2020-11-28 18:01  fc -lE

如果只想显示时间,请使用 -d 旗帜:

% fc -ld

样本输出:

    1  17:00  lsb_release -a
    2  17:00  uname -r
    3  17:00  hostname
    4  17:00  ls -l
    5  17:00  touch ostechnix.txt
    6  17:00  clear
    7  17:01  history
    8  17:06  history -f
    9  17:18  history -E
   10  17:18  history -i
   11  17:19  history -d
   12  17:43  fc -lf
   13  18:01  fc -lE
   14  18:02  fc -li

您还可以从历史记录中的特定条目开始显示带有时间戳的历史记录输出。 例如,要列出从 第 5 名 在 zsh 中带有时间戳的命令,运行:

% fc -li 5

样本输出:

    5  2020-11-28 17:00  touch ostechnix.txt
    6  2020-11-28 17:00  clear
    7  2020-11-28 17:01  history
    8  2020-11-28 17:06  history -f
    9  2020-11-28 17:18  history -E
   10  2020-11-28 17:18  history -i
   11  2020-11-28 17:19  history -d
   12  2020-11-28 17:43  fc -lf
   13  2020-11-28 18:01  fc -lE
   14  2020-11-28 18:02  fc -li
   15  2020-11-28 18:05  fc -ld

有关更多详细信息,请参阅手册页:

% man zsh
% man fc

你正在用吗 贝壳? 检查以下指南以在 Fish shell 中启用时间戳:

  • 在 Fish Shell 中启用历史命令的时间戳