天天看點

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  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        

例二: vi /root/.bash_history

例三:

 1、修改/etc/profile将HISTSIZE=1000改成0或1

  清除使用者home路徑下。bash_history

  2、立即清空裡的history目前曆史指令的記錄

  history -c

繼續閱讀