在Linux中的Zsh中启用历史记录命令的时间戳
在历史命令输出中启用时间戳有助于我们发现在Linux中执行特定命令的时间。 我们已经看到了如何 在Bash历史记录中启用时间戳。 今天,让我们看看如何启用时间戳 history
Linux中的Zsh Shell中的命令。
在Zsh Shell中为历史记录命令启用时间戳记
您可以使用以下命令在Linux中的Bash历史记录输出中显示日期和时间戳记: HISTTIMEFORMAT
环境变量。 但是,您不需要在Zsh中设置任何env变量。 默认情况下,Zsh具有一些内置标志来显示历史命令输出中的日期和时间。
首先,让我们查看先前执行的命令列表 zsh
会话使用 history
命令:
% history
样本输出:
1 lsb_release -a
2 uname -r
3 hostname
4 ls -l
5 touch ostechnix.txt
6 clear
zsh会话中的history命令输出
如您所见, 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
命令
在zhs shell中的历史记录输出中启用时间戳的另一种方法是使用 fc
命令。 的 fc
命令,简称 Fix Commands是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
标志会在中打印完整的时间日期戳 美国格式 即 '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中使用带有时间戳的命令,运行:
% 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
历史命令LinuxLinux基础Linux命令Linux技巧Shell Timestamps Z shell ZSH