Linux History记录加时间戳实用技巧

使用过linux的人都一定知道使用history可以输出你曾经输入过的历史命令,例如  
[root@server ~]# history  |  more  
6  cd /root  
7  vim test1.sh   
 

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

通过设置环境变量 export HISTTIMEFORMAT="%F %T `whoami` " 给history加上时间戳  
      
[root@server ~]# export HISTTIMEFORMAT="%F %T `whoami` "  
[root@server ~]# history
  993  2016-04-20 10:40:59 root echo 'export HISTTIMEFORMAT="%F %T `whoami` "'
  994  2016-04-20 10:40:59 root history
  995  2016-04-20 10:40:59 root ls
  996  2016-04-20 10:40:59 root history
  997  2016-04-20 10:40:59 root echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile
  998  2016-04-20 10:40:59 root history
  999  2016-04-20 10:40:59 root exit
 1000  2016-04-20 10:41:00 root history
 1001  2016-04-20 10:41:02 root ls
 1002  2016-04-20 10:41:13 root cat /etc/profile
 1003  2016-04-20 10:51:23 root history
 1004  2016-04-20 10:51:33 root ll
 1005  2016-04-20 10:51:35 root history
      
可以看到,历史命令的时间戳已经加上了,但是.bash_history里并没有加上这个时间戳。其实这个时间记录是保存在当前shell进程内存里的,如果你logout并且重新登录的话会发现你上次登录时执行的那些命令的时间戳都为同一个值,即当时logout时的时间。  

尽管如此,对于加上screen的bash来说,这个时间戳仍然可以长时间有效的,毕竟只要你的server不重启,screen就不会退出,因而这些时间就能长时间保留。

你也可以使用echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile 然后source /etc/profile一下就OK
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值