1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > linux执行命令带时间戳 Linux查看History记录加时间戳的小技巧

linux执行命令带时间戳 Linux查看History记录加时间戳的小技巧

时间:2019-12-19 07:29:55

相关推荐

linux执行命令带时间戳 Linux查看History记录加时间戳的小技巧

Linux查看History记录加时间戳小技巧

熟悉bash的都一定知道使用history可以输出你曾经输入过的历史命令,例如[root@servyou_web ~]# history | more 6 ./test.sh 7 vim test.sh 8 ./test.sh

但是这里只显示了命令,并没有显示执行命令的时间,因为保存历史命令的~/.bash_history里并没有保存时间。

通过设置环境变量 export HISTTIMEFORMAT="%F %T `whoami` " 给history加上时间戳[root@servyou_web ~]# export HISTTIMEFORMAT="%F %T `whoami` " [root@servyou_web ~]# history | tail 1014 -06-22 19:17:29 root 15 -06-22 19:13:02 root ./test.sh 1015 -06-22 19:17:29 root 16 -06-22 19:13:02 root vim test.sh 1016 -06-22 19:17:29 root 17 -06-22 19:13:02 root ./test.sh 1017 -06-22 19:17:29 root 18 -06-22 19:13:02 root vim test.sh 1018 -06-22 19:17:29 root 19 -06-22 19:13:02 root ./test.sh 1019 -06-22 19:17:29 root 20 -06-22 19:13:02 root vim test.sh 1020 -06-22 19:17:29 root 21 -06-22 19:13:02 root ./test.sh 1021 -06-22 19:17:29 root 22 -06-22 19:13:02 root vim test.sh 1022 -06-22 19:25:22 root 22 -06-22 19:13:02 root vim test.sh 1023 -06-22 19:25:28 root history | tail

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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对网页设计的支持。如果你想了解更多相关内容请查看下面相关链接

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。