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里并沒有加上這個時間戳。其實這個時間記錄是保存在當前shell進程內(nèi)存里的,如果你logout并且重新登錄的話會發(fā)現(xiàn)你上次登錄時執(zhí)行的那些命令的時間戳都為同一個值,即當時logout時的時間。

盡管如此,對于加上screen的bash來說,這個時間戳仍然可以長時間有效的,畢竟只要你的server不重啟,screen就不會退出,因而這些時間就能長時間保留。你也可以使用echo ‘export HISTTIMEFORMAT=”%F %T `whoami` “‘ >> /etc/profile 然后source一下就OK

贊(4)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-62778877-8306;郵箱:fanjiao@west.cn。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:西部數(shù)碼知識庫 » Linux查看History記錄加時間戳的小技巧

登錄

找回密碼

注冊