history命令詳解


Linux下history命令用法

^_^在項目中希望調用history命令來獲取用戶的歷史記錄,方便分析,可是我們平時所見到的history結果是下面這樣:

# history | head -10
   30  rm -fr lala
   31  for i in `seq 1000`; do
   32  sleep 1
   33  for i in `seq 1000`; do sleep 10
   34  cd /home/
   35  ll
   36  cd lib/
   37  ll
   38  cd ..
   39  ll

貌似達不到我們想要的效果,顯示具體時間,具體命令的效果,那現在讓我們一起來探索一下history命令的奧妙吧!

使用 HISTTIMEFORMAT 顯示時間戳

# export HISTTIMEFORMAT='%F %T'
# history | more
    1  2016-08-01 14:47:55ls
    2  2016-08-01 14:47:55vim /etc/sudoers
    3  2016-08-01 14:47:55ls
    4  2016-08-01 14:47:55cd /home/ruanyang/
    5  2016-08-01 14:47:55ls
    6  2016-08-01 14:47:55cd 下載
    7  2016-08-01 14:47:55ls
    8  2016-08-01 14:47:55cd ctags-5.8/

備注:這個環境變量的聲明只能作用於當前的bash,所以如果長久有效的話,需要將其定義到/etc/profile文件中

使用 HISTSIZE 控制歷史命令記錄的總行數

將這兩行內容追加到/etc/profile文件中,當你再次重新登錄bash時,歷史命令總行數會變成100000

export HISTSIZE=100000
export HISTFILESIZE=100000

備注:如果我們想要看到更多的歷史命令時,不妨使用這個變量

使用 HISTFILE 更改歷史文件名稱

默認情況下,歷史命令存放在~/.bash_history文件中。如下,重新定位歷史命令存放位置

export HISTFILE=/.logs/history_${LOGNAME}

備注:這樣可以將每個用戶的歷史文件清晰的使用文件名來標記,方便分析

使用 HISTCONTROL 從命令歷史中剔除連續重復的條目

HISTCONTROL=ignoredups剔除連續的相同命令的條目,僅剩余一條,如下:

# export HISTCONTROL=ignoredups
# cd
# cd
# cd

我們現在來看看效果吧!

# history | tail -n 5
  133  2016-08-01 17:51:30history 
  134  2016-08-01 18:09:54export HISTCONTROL=ignoredups
  135  2016-08-01 18:09:59cd
  136  2016-08-01 18:10:04history 
  137  2016-08-01 18:22:12history | tail -n 5

三個cd變成一個了

使用 HISTCONTROL 清除整個命令歷史中的重復條目

HISTCONTROL=erasedups歷史中的重復命令被清除到僅剩距離當前時間最近的那條命令

# export HISTCONTROL=erasedups
# pwd
# cd
# ls
# pwd
# pwd

看看效果吧!pwd剔除剩一個啦

# history | tail -10
  100  2016-08-01 18:34:47export HISTCONTROL=erasedups
  101  2016-08-01 18:34:58cd
  102  2016-08-01 18:34:58ls
  103  2016-08-01 18:35:07pwd
  104  2016-08-01 18:35:20history | tail -10

使用 HISTCONTROL 強制 history 不記住特定的命令

HISTCONTROL=ignorespace,在不想被記住的命令前面輸入一個空格

$ export HISTCONTROL=ignorespace
$ ls
$  cd
$ pwd

注意,我在cd前輸入了一個空格,我們一起來看看效果吧!cd消失啦!

$ history | tail -5
 1997  more sources.list
 1998  export HISTCONTROL=ignorespace
 1999  ls
 2000  pwd
 2001  history | tail -5

使用HISTSIZE禁用history

如果想禁用history,可以將HISTSIZE設置為0:

$ export HISTSIZE=0
$ history 

使用HISTIGNORE忽略歷史中的特定命令

忽略pwdls命令:

$ export HISTIGNORE="pwd:ls:"
$ pwd
$ cd
$ ls

看看效果吧!

$ history 4
 1998  export HISTIGNORE="pwd:ls:"
 1999  cd
 2000  history 5

快捷鍵-使用CTRL+R搜索歷史

比較簡單,自己動手試一下吧!

從命令歷史中執行一個指定的命令

!number

eg.!4

指定關鍵字來執行以前的命令

輸入!ps並回車,將執行以ps打頭的命令

快速重復執行上一條命令

以下四種方法,上方向鍵、!!!-1crtl+p

$ history 4
 1998  export HISTIGNORE="pwd:ls:"
 1999  cd
 2000  history 5
 2001  history 4
$ !!
history 4
 1998  export HISTIGNORE="pwd:ls:"
 1999  cd
 2000  history 5
 2001  history 4
$ !-1
history 4
 1998  export HISTIGNORE="pwd:ls:"
 1999  cd
 2000  history 5
 2001  history 4
$ history 4
 1998  export HISTIGNORE="pwd:ls:"
 1999  cd
 2000  history 5
 2001  history 4

命令替換

!!:$!$都可以為當前命令獲得上一條命令的參數
!^獲得上一條命令的第一項參數

演示:

$ ls go 
go
$ cat !$		#!!:$也可以實現
cat go
#!/home/s/ops/perl/bin/perl
......

History命令語法

# history [n]
# history [-c]
# history [-raw] histfiles

參數:
n :數字,要列出最近的 n 條命令列表
-c :將目前的shell中的所有 history 內容全部消除
-a :將目前新增的history 指令新增入 histfiles 中,若沒有加 histfiles ,則預設寫入 ~/.bash_history
-r :將 histfiles 的內容讀到目前這個 shell 的 history 記憶中
-w :將目前的 history 記憶內容寫入 histfiles

Linux系統當你在shell(控制台)中輸入並執行命令時,shell會自動把你的命令記錄到歷史列表中,一般保存在用戶目錄下的.bash_history文件中。默認保存1000條,你也可以更改這個值。

如果你鍵入 history, history會向你顯示你所使用的前1000個歷史命令,並且給它們編了號,你會看到一個用數字編號的列表快速從屏幕上卷過。你可能不需要查看1000個命令中的所有項目, 當然你也可以加入數字來列出最近的 n 筆命令列表。

linux中history命令不僅僅讓我們可以查詢歷史命令而已. 我們還可以利用相關的功能來幫我們執行命令。

運行特定的歷史命令

history會列出bash保存的所有歷史命令,並且給它們編了號,我們可以使用“嘆號接編號”的方式運行特定的歷史命令.
語法說明:

[test@linux]# [!number]  [!command] [!!]

參數說明:
number :第幾個指令的意思;
command :指令的開頭幾個字母
! :上一個指令的意思!

參考文章:
http://blog.sina.com.cn/s/blog_5caa94a00100gyls.html


免責聲明!

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



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