Linux歷史命令

Linux歷史命令

敲過的命令,Linux是會有記錄的,預設能夠記錄1000條歷史命令。這些命令保存在用戶的家目錄中的.bash_history文件中。只有當用戶正常退出當前shell時,在當前shell中運行的命令纔會保存至.bash_history文件中。css

[root@centos-01 ~]# ls /root/.bash_history 
/root/.bash_history
[root@centos-01 ~]# cat !$

history命令若是未改動過環境變量,默承認以把最近1000條歷史命令打印出來。linux

[root@centos-01 ~]# history

歷史命令條數由環境變量HISTSIZE決定的

[root@centos-01 ~]# echo $HISTSIZE
1000

能夠在/etc/profile文件中修改環境變量HISTSIZEshell

[root@centos-01 ~]# vi /etc/profile

若是修改了環境變量HISTSIZE,並想使之生效,那麼須要從新進入終端。或者執行命令source /etc/profile使之生效。vim

[root@centos-01 ~]# source /etc/profile

清空當前內存中的歷史命令

[root@centos-01 ~]# history -c
[root@centos-01 ~]# history
    1  history
[root@centos-01 ~]# cat .bash_history

能夠看到,文件中的歷史命令並無清空centos


修改歷史命令保存的格式

[root@centos-01 ~]# history
    1  history
    2  cat .bash_history 
    3  vi /etc/profile
    4  source /etc/profile 
    5  source /etc/profile
    6  $HISTSIZE
    7  echo $HISTSIZE
    8  history
[root@centos-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@centos-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@centos-01 ~]# history 
    1  2018/05/06 00:11:46 history
    2  2018/05/06 00:12:57 cat .bash_history 
    3  2018/05/06 00:16:12 vi /etc/profile
    4  2018/05/06 00:18:16 source /etc/profile 
    5  2018/05/06 00:18:34 source /etc/profile
    6  2018/05/06 00:19:41 $HISTSIZE
    7  2018/05/06 00:19:57 echo $HISTSIZE
    8  2018/05/06 00:20:27 history
    9  2018/05/06 00:24:36 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
   10  2018/05/06 00:24:54 echo $HISTTIMEFORMAT
   11  2018/05/06 00:28:57 history

在新打開的終端中,再次查看環境變量HISTTIMEFORMATruby

[root@centos-01 ~]# echo $HISTTIMEFORMAT

[root@centos-01 ~]#

發現以前修改環境變量HISTTIMEFORMAT,只對當前終端有效,對其餘終端無效。bash


要想使設置的環境變量HISTTIMEFORMAT有效,能夠編輯/etc/profile文件。markdown

[root@centos-01 ~]# vim /etc/profile

能夠在HISTSIZE=5000下面另起一行,添加spa

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

保存退出code

[root@centos-01 ~]# source !$
source /etc/profile

再新打開一個終端,查看環境變量HISTTIMEFORMAT

[root@centos-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S

永久保存歷史命令,能夠增長隱藏權限

chattr +a ~/.bash_history

  • !!」連續兩個‘!’,表示執行上一條指令
[root@centos-01 ~]# ls
anaconda-ks.cfg
[root@centos-01 ~]# !!
ls
anaconda-ks.cfg
  • 「!n」(n是數字),表示執行歷史命令中第n條指令。例如「!1002」,表示執行歷史命令中第1002條命令。

  • 「!字符串」(字符串大於等於1),倒着找歷史命令中第一個以該字符串開頭的命令。例如「!echo」,表示執行歷史命令中最近一次以「echo」開頭的指令。

相關文章
相關標籤/搜索