Linux History记录添加时间戳

熟悉bash的都一定知道使用history可以输出你曾经输入过的历史命令,例如 :
[root@YAN ~]# history | more
6 ./test.sh
7 vim test.sh
8 ./test.sh
但是这里只显示了命令,并没有显示执行命令的时间,因为保存历史命令的~/.bash_history里并没有保存时间。

通过设置环境变量 export HISTTIMEFORMAT=”`whoami` %F %T ” 给history加上时间戳

[root@YAN ~]# export HISTTIMEFORMAT=”`whoami` %F %T ”
[root@YAN ~]# history | tail
1014 root 2011-06-22 19:17:29 ./test.sh
1015 root 2011-06-22 19:17:29 vim test.sh
1016 root 2011-06-22 19:17:29 ./test.sh
1017 root 2011-06-22 19:17:29 vim test.sh
1018 root 2011-06-22 19:17:29 ./test.sh
1019 root 2011-06-22 19:17:29 vim test.sh
1020 root 2011-06-22 19:17:29 ./test.sh
1021 root 2011-06-22 19:17:29 vim test.sh
1022 root 2011-06-22 19:25:22 vim test.sh
1023 root 2011-06-22 19:25:28 history | tail

可以看到,历史命令的时间戳已经加上了,但是.bash_history里并没有加上这个时间戳。其实这个时间记录是保存在当前shell进程内存里的,如果你logout并且重新登录的话会发现你上次登录时执行的那些命令的时间戳都为同一个值,即当时logout时的时间。

尽管如此,对于加上screen的bash来说,这个时间戳仍然可以长时间有效的,毕竟只要你的server不重启,screen就不会退出,因而这些时间就能长时间保留。你也可以使用echo ‘export HISTTIMEFORMAT=”`whoami` %F %T “‘ >> /etc/profile

source /etc/profile 一下就OK.

欢迎关注下方“非著名资深码农“公众号进行交流~

发表评论

邮箱地址不会被公开。