优选主流主机商
任何主机均需规范使用

history查看命令执行时间

Linux系统中,history命令可以显示历史命令记录。

默认情况下,history命令的输出格式为:

[cnbanwagong@localhost ~]$ history
1 cat /etc/centos-release
2 uname -r
3 poweroff
4 bye
5 logout
6 ls -la
7 history

工作中用于排查相关问题的时候,会使用history命令来查看执行命令的记录。有时候需要知道命令执行的时间,这就用到了变量HISTTIMEFORMAT。

编辑文件~/.bash_profile

[cnbanwagong@localhost ~]$ vi .bash_profile
HISTTIMEFORMAT=”%F %T ”
export HISTTIMEFORMAT

使用source命令让更改立即生效

[cnbanwagong@localhost ~]$ source .bash_profile
[cnbanwagong@localhost ~]$ history
1 2021-07-17 13:37:01 cat /etc/centos-release
2 2021-07-17 13:37:01 uname -r
3 2021-07-17 13:37:01 poweroff
4 2021-07-17 13:37:01 bye
5 2021-07-17 13:37:01 logout
6 2021-07-17 13:37:13 ls -la
7 2021-07-17 13:37:19 history
8 2021-07-17 13:38:04 vi .bash_profile
9 2021-07-17 13:39:32 history
10 2021-07-17 13:40:05 vim .bash_profile
11 2021-07-17 13:40:09 vi .bash_profile
12 2021-07-17 13:49:35 history
13 2021-07-17 13:49:52 source .bash_profile
14 2021-07-17 13:49:54 history

这样再执行history命令,就可以看到命令执行的具体时间了。

未经允许不得转载:搬瓦工中文网 » history查看命令执行时间