CentOS 顯示歷史執行過的命令以及用戶歷史命令緩存文件


1、history命令用於顯示歷史執行過的命令

執行 history命令能顯示出當前用戶在本地計算機中執行過的最近 1000 條命令記錄。

如果覺得 1000 不夠用,還可以自定義/etc/profile 文件中的HISTSIZE 變量值。

在使用 history 命令時,如果使用-c 參數則會清空所有的命令歷史記錄。

[root@bigdata-senior01 /]# history
   36  cat ~/.bash_history
   37  history
   38  cat ~/.bash_history
   39  cat ~/.bash_history | less
   40  history
   41  ls
   42  free -h
   43  history

2、使用“!編碼數字”的方式來重復執行某一次的命令。

[root@bigdata-senior01 /]# !42
free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        156M        1.5G        8.5M        178M        1.5G
Swap:          2.0G          0B        2.0G

 

3、歷史命令會被保存到用戶家目錄中的.bash_history 文件中。Linux 系統中以點(.)開頭的文件均代表隱藏文件,這些文件大多數為系統服務文件,可以用 cat 命令查看其文件內容。

查看自己的home下的用戶目錄的歷史命令記錄
“~”波浪線,表示用戶家目錄
[root@bigdata-senior01 /]# cat ~/.bash_history
~”波浪線:
[root@bigdata-senior01 ~]# su - es 上一次登錄:六 1月 5 21:48:54 CST 2019pts/0 上 [es@bigdata-senior01 ~]$ pwd /home/es [es@bigdata-senior01 ~]$ cd / [es@bigdata-senior01 /]$ ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [es@bigdata-senior01 /]$ pwd / [es@bigdata-senior01 /]$ cd ~ [es@bigdata-senior01 ~]$ pwd /home/es

 4、在histrory記錄中加入時間戳

export HISTTIMEFORMAT="%F %T "

[root@bigdata-senior01 ~]# export | grep HIST
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HISTTIMEFORMAT="%F %T "

history顯示:

 1024  2019-01-05 22:17:21 export HISTTIMEFORMAT="%F %T "
 1025  2019-01-05 22:17:24 history
 1026  2019-01-05 22:19:00 export
 1027  2019-01-05 22:19:09 export | grep HIST
 1028  2019-01-05 22:19:41 history | less
 1029  2019-01-05 22:19:53 history

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM