最近 x 个月内最常用的命令

Most frequently used commands during the last x months

我知道如何使用

在 zsh 中获取最常用的 shell 命令

history 1 | awk '{="";print substr([=10=],2)}' | sort | uniq -c | sort -n | tail -n 20

但是有什么方法可以限制我自己只说过去两三个月吗?

我需要这个,因为我想为我目前使用最多的命令创建别名。

history 在 zsh 中有几个标志来显示日期和时间戳。为此,您必须将 setopt extended_history 添加到 .zshrc 文件中。

如果您启用了 extended_historyhistory -i 将以 ISO8601 `yyyy-mm-dd hh:mm' 格式显示完整的时间-日期戳。这种格式的日期可以作为字符串进行比较。因此,只需更改您的 awk 脚本并将其用于 select 仅在某个日期后的行。

history -i 1 | awk '{ if ( >= "2020-05-01") { ==="";print [=10=]; } }'  | sort | uniq -c | sort -n -r | head -n 20

请注意,如果您启用了 HIST_IGNORE_ALL_DUPSHIST_IGNORE_DUPS 选项,这将无法正常工作。

您也可以使用date命令自动获取旧日期。