歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux命令 >> linux查看歷史命令history

linux查看歷史命令history

日期:2017/2/28 9:58:23   编辑:Linux命令

1. history作用

linux的history命令的作用是,記錄執行過的命令。 用法: history [n] n為數字,列出最近的n條命令 -c 將目前shell中的所有history命令消除 history [-raw] histfiles -a 將目前新增的命令寫入histfiles, 默認寫入~/.bash_history -r 將histfiles內容讀入到目前shell的history記憶中 -w 將目前history記憶的內容寫入到histfiles
shell > history
   34  14-10-28 16:19:24 ll
   35  14-10-28 16:19:26 vim test
   36  14-10-28 16:19:33 ll
   37  14-10-28 16:19:34 ll
展示3行
shell > history 3
 1032  14-11-02 16:10:41 history
 1033  14-11-02 16:10:46 history |more
 1034  14-11-02 16:11:15 history 3
使用! 執行歷史命令。 ! number 執行第幾條命令 ! command 從最近的命令查到以command開頭的命令執行 !! 執行上一條
shell > !1046
history 3
 1045  14-11-02 16:22:38 head ~/.bash_history 
 1046  14-11-02 16:35:37 history 3
 1047  14-11-02 16:35:48 history 3
shell > !!
history 3
 1046  14-11-02 16:35:37 history 3
 1047  14-11-02 16:35:48 history 3
 1048  14-11-02 16:35:52 history 3
shell > !head
head ~/.bash_history 
#1414484377
cd update/
#1414484377

2. history配置修改

history記錄的行數
shell > echo $HISTSIZE
1000
shell > 
默認記錄1000行 配置文件在/etc/profile中修改
HISTSIZE=1000
export  HISTSIZE
歷史命令文件記錄在 ~/.bash_history中
想要讓linux的history命令顯示時間,history是默認不帶時間, 在/etc/profile 中增加
export HISTTIMEFORMAT="%y-%m-%d %H:%M:%S "
查看.bash_history
shell > head ~/.bash_history 
#1414484377
cd update/
#1414484377
ll
#1414484388
vim address 
#1414484439
ll
#1414484440
ll
shell > 

3. 同一賬號同時多次登錄寫入history

普通情況下, 當以bash登錄系統時,系統會從~/.bash_history讀取以前運行的命令 當注銷時,把最新的1000(HISTSIZE)條命令更新到~/.bash_history文件中。 也可以使用history -w強制立刻寫入,僅保留最新的。
當同一賬號,同時登錄多個bash時,只有最後一個退出的會寫入bash_history,其他的都被覆蓋了。

4. Ctrl+r 反向查詢歷史命令

使用Ctrl+r反向查詢歷史命令,將匹配的最新一條顯示出來 如果還想繼續向上查詢,繼續按Ctrl+r
shell > history 3                                       
 1048  14-11-02 16:35:52 history 3
 1049  14-11-02 16:36:11 head ~/.bash_history 
 1050  14-11-02 16:41:05 history 3
(reverse-i-search)`his': head ~/.bash_history 
地址:http://blog.csdn.net/yonggang7/article/details/40710623
Copyright © Linux教程網 All Rights Reserved