Linux查看History記錄加時間戳小技巧
熟悉bash的都一定知道使用history可以輸出你曾經(jīng)輸入過的歷史命令,例如
[root@servyou_web ~]# history | more
6 ./test.sh
7 vim test.sh
8 ./test.sh
但是這里只顯示了命令,并沒有顯示執(zhí)行命令的時間,因為保存歷史命令的~/.bash_history
里并沒有保存時間。
通過設(shè)置環(huán)境變量 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
可以看到,歷史命令的時間戳已經(jīng)加上了,但是.bash_history里并沒有加上這個時間戳。其實這個時間記錄是保存在當(dāng)前shell進(jìn)程內(nèi)存里的,如果你logout并且重新登錄的話會發(fā)現(xiàn)你上次登錄時執(zhí)行的那些命令的時間戳都為同一個值,即當(dāng)時logout時的時間。
盡管如此,對于加上screen的bash來說,這個時間戳仍然可以長時間有效的,畢竟只要你的server不重啟,screen就不會退出,因而這些時間就能長時間保留。你也可以使用echo 'export HISTTIMEFORMAT="%F %T `whoami` "'
>> /etc/profile
然后source
一下就OK
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接