在 Linux 中的 Zsh 中启用历史命令的时间戳
在历史命令输出中启用时间戳有助于我们查找 Linux 中某个命令的执行时间。我们已经了解了如何在 Bash 历史记录中启用时间戳。今天让我们看看如何在 Linux 的 Zsh shell 中为 history 命令启用时间戳。
在 Zsh Shell 中为历史命令启用时间戳
您可以使用 HISTTIMEFORMAT 环境变量在 Linux 中的 Bash 历史输出中显示日期和时间戳。但是,您不需要在 Zsh 中设置任何环境变量。默认情况下,Zsh 有一些内置标志来显示历史命令输出中的日期和时间。
首先,让我们使用 history 命令查看 zsh 会话中之前执行的命令列表:
% history示例输出:
1 lsb_release -a
2 uname -r
3 hostname
4 ls -l
5 touch ostechnix.txt
6 clear
zsh 会话中的历史命令输出
如您所见,history 命令不显示时间戳。它只显示每个命令的前缀号。
要为历史命令启用时间戳,即在 zsh shell 中的所有命令中显示日期和时间,请在 history 命令中使用 -f 标志:
% history -f现在您将在 zsh shell 的 history 命令输出中看到日期和时间:
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 命令是 fix c 命令的缩写,是一个 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 命令显示历史输出中的日期和时间戳
如前所述,-f 标志以 US 格式 打印完整的时间日期戳,即 '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要以 ISO8601 格式(即 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您还可以显示历史记录输出,其中包含从历史记录中的特定条目开始的时间戳。例如,要列出从 zsh 中的第 5 个命令开始并带有时间戳的历史输出,请运行:
% 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壳吗?检查以下指南以在 Fish shell 中启用时间戳:
- 在 Fish Shell 中启用历史命令的时间戳


