history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 參數 [參數...]
history的作用是顯示或操縱歷史列表。
相關選項:
-c 刪除所有條目從而清空歷史列表。
使用 HISTTIMEFORMAT 顯示時間戳
當你從命令行執行 history 命令后,通常只會顯示已執行命令的序號和命令本身。如果你想要查看命令歷史的時間戳,那么可以執行:
1 [root@localhost ~]# export HISTTIMEFORMAT='%F %T ' #使用 HISTTIMEFORMAT 顯示時間戳 2 [root@localhost ~]# history | tail -5 #顯示最近5條命令,可以從上面看到命令的使用時間記錄 3 22 2019-03-20 02:36:33 ls 4 23 2019-03-20 02:38:05 export HISTTIMEFORMAT='%F %T ' 5 24 2019-03-20 02:38:09 history 6 25 2019-03-20 02:41:36 export HISTTIMEFORMAT='%F %T ' 7 26 2019-03-20 02:41:44 history | tail -5
快速重復執行上一條命令
有 4 種方法可以重復執行上一條命令:
1. 使用上方向鍵,並回車執行。
2. 按 !! 並回車執行。
3. 輸入 !-1 並回車執行。
4. 按 Ctrl+P 並回車執行。
從命令歷史中執行一個指定的命令
在下面的例子中,如果你想重復執行第 31 條命令,那么可以執行 !31:
1 [root@localhost ~]# history | tail -5 #顯示最近5條記錄 2 28 2019-03-20 02:46:49 pwd 3 29 2019-03-20 02:47:05 history | tail -5 4 30 2019-03-20 02:47:09 ls 5 31 2019-03-20 02:47:16 pwd 6 32 2019-03-20 02:48:35 history | tail -5 7 [root@localhost ~]# !31 #執行第31條命令 8 pwd 9 /root 10 [root@localhost ~]#
通過指定關鍵字來執行以前的命令
在下面的例子,輸入 !history 並回車,將執行以 history 打頭的命令:
1 [root@localhost ~]# !history #顯示並執行以history打頭的命令 2 history | tail -5 #執行history | tail -5命令 3 30 2019-03-20 02:47:09 ls 4 31 2019-03-20 02:47:16 pwd 5 32 2019-03-20 02:48:35 history | tail -5 6 33 2019-03-20 02:48:46 pwd 7 34 2019-03-20 02:53:50 history | tail -5 8 [root@localhost ~]#