在 linux 中,查看每条命令的执行时间。
配置 shell 以记录命令执行时间
bash shell
bash 是最常用的 shell 之一,通过配置 histtimeformat
环境变量,可以在历史记录中包含时间戳。
步骤:
检查当前历史记录设置
首先,查看当前的 histtimeformat
是否已设置:
echo $histtimeformat
如果返回为空,说明尚未启用时间戳。
启用时间戳
编辑你的 ~/.bashrc
文件,添加以下行:
export histtimeformat="%f %t "
解释:
%f
表示日期(年-月-日)。%t
表示时间(时:分:秒)。
应用更改
执行以下命令以使更改生效:
source ~/.bashrc
查看历史记录
使用 history
命令查看带有时间戳的历史记录:
history
示例输出:
1 2024-04-26 10:15:30 ls -la 2 2024-04-26 10:16:05 cd /var/www 3 2024-04-26 10:16:20 vim index.html
注意事项
历史记录大小:确保 histsize
和 histfilesize
设置足够大,以保存足够的历史记录。例如,在 ~/.bashrc
中添加:
export histsize=10000 export histfilesize=20000
安全性:时间戳会记录每个命令的执行时间,这可能涉及隐私或安全问题。请根据需要调整权限和可访问性。
zsh shell
如果你使用的是 zsh,同样可以配置时间戳。
步骤:
启用时间戳
编辑你的 ~/.zshrc
文件,添加以下行:
hist_stamps="yyyy-mm-dd"
你也可以使用其他格式,如 "mm/dd/yyyy"
或 "iso"
。
应用更改
执行以下命令以使更改生效:
source ~/.zshrc
查看历史记录
使用 history
或 fc -l
命令查看带有时间戳的历史记录:
history # 或 fc -l
示例输出:
1 2024-04-26 ls -la 2 2024-04-26 cd /var/www 3 2024-04-26 vim index.html
注意事项
历史记录设置:确保 histsize
和 savehist
设置足够大。例如,在 ~/.zshrc
中添加:
histsize=10000 savehist=20000
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论