首页 技术 正文
技术 2022年11月18日
0 收藏 669 点赞 4,746 浏览 1184 个字

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 2011-06-22 19:17:29 root 15 2011-06-22 19:13:02 root ./test.sh
1015 2011-06-22 19:17:29 root 16 2011-06-22 19:13:02 root vim test.sh
1016 2011-06-22 19:17:29 root 17 2011-06-22 19:13:02 root ./test.sh
1017 2011-06-22 19:17:29 root 18 2011-06-22 19:13:02 root vim test.sh
1018 2011-06-22 19:17:29 root 19 2011-06-22 19:13:02 root ./test.sh
1019 2011-06-22 19:17:29 root 20 2011-06-22 19:13:02 root vim test.sh
1020 2011-06-22 19:17:29 root 21 2011-06-22 19:13:02 root ./test.sh
1021 2011-06-22 19:17:29 root 22 2011-06-22 19:13:02 root vim test.sh
1022 2011-06-22 19:25:22 root 22 2011-06-22 19:13:02 root vim test.sh
1023 2011-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

下一篇: poj 1129 搜索
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781