6 ./test.sh
7 vim test.sh
8 ./test.sh

但是這里只顯示了命令,并沒有顯示執(zhí)行命令的時(shí)間,因?yàn)楸4鏆v史命令的~/.bash_history里并沒有保存時(shí)間。

通過設(shè)置環(huán)境變量 export HISTTIMEFORMAT=”%F %T `whoami` ” 給history加上時(shí)間戳

[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

可以看到,歷史命令的時(shí)間戳已經(jīng)加上了,但是.bash_history里并沒有加上這個(gè)時(shí)間戳。其實(shí)這個(gè)時(shí)間記錄是保存在當(dāng)前shell進(jìn)程內(nèi)存里的,如果你logout并且重新登錄的話會(huì)發(fā)現(xiàn)你上次登錄時(shí)執(zhí)行的那些命令的時(shí)間戳都為同一個(gè)值,即當(dāng)時(shí)logout時(shí)的時(shí)間。

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

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

登錄

找回密碼

注冊(cè)