history命令查看执行命令的时间

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

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

[zhangchuangfei@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

[zhangchuangfei@localhost ~]$ vi .bash_profile
HISTTIMEFORMAT="%F %T "
export HISTTIMEFORMAT

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

[zhangchuangfei@localhost ~]$ source .bash_profile
[zhangchuangfei@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命令,就可以看到命令执行的具体时间了。

上一篇:android学习笔记---37_采用广播接收者实现系统短信操作_获取短信_收发短信等


下一篇:剑指 Offer 37. 序列化二叉树