小屋創作

日誌2019-05-08 01:22

Ubuntu history 指令用法教學

作者:GJLMoTea



$ history

history 是 Linux中重要且好用的內建功能,可以記錄最近下過的指令,並且透過按↑、↓方向鍵選擇所執行過的指令。

$ echo $HISTFILE
用於查看HISTFILE的位置,$HISTFILE變數存放history file的位置。
在bash shell 底下 ,history 的檔案是 ~/.bash_history 就是家目錄(/home/user)底下的一個隱藏檔。

(~/.bash_history)為 history的檔案,
當開啟一個 bash shell(terminal或稱session)時會讀取檔案到記憶體(memory)中,
在關閉 terminal 之前都是在記憶體中操作,
直到退出 terminal 才會貼回(append)到HISTFILE檔案、儲存到硬碟(disk)中。

當同時開啟多個terminal,彼此間執行的command不會互相看見。
(可以設置 PROMPT_COMMAND 指令參數 來達成互相看見)


history的基本指令:

$ !123
執行history中的第123個指令

$ history -d 123
可刪除第123行的history command

$ history 10
列出最近的10筆指令

$ history -c
在mem中清除此terminal執行過的command history

$ history -a $ history -w
立刻將在mem中的command history寫入同步到 ~/.bash_history 檔案中
(-a : append,有一個指針記錄append到哪一行)
(-w : write,覆寫 ~/.bash_history 檔案)

$ history -r
複製 ~/.bash_history 中的history command(disk),append到history list(mem)中(關閉terminal時將不會自動寫回~/.bash_history 檔案中)

$ history -n
從 ~/.bash_history 中的history command(disk) 讀取尚未讀取的所有紀錄(因為~/.bash_history可能被修改過),append到history list(mem)中



history的參數設定:

$ vim ~/.bashrc

HISTTIMEFORMAT='%F %T '
用於添加日期及時間
(%F: %Y-%m-%d)
(%T: %H:%M:%S)
設置完後會在(~/.bash_history)檔案中插入#timestamp

HISTCONTROL=ignoredups:ignorespace 或者 HISTCONTROL=ignoreboth
ignoredups:重複連續的指令不會記錄
ignorespace:在執行的command前加上空白,history將不會記錄這行指令

HISTSIZE=100
結束terminal時,會儲存在這個terminal中執行過的command(至多儲存最近的100行)到~/.bash_history中
同時也是 $ history 印出的數量100command、用方向鍵↑至多溯及100行command
當HISTSIZE設為0時將不留記錄往後執行的command也無法使用方向鍵↑↓來選擇command,已存在在~/.bash_history中的指令仍然存在

HISTFILESIZE=1000
~/.bash_history 檔案中只能儲存至多1000行資料,超過1000行時會移除最舊的資料


多個terminal間彼此能看到執行的command的方法( HISTFILE 與每個terminal同步)
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"PROMPT_COMMAND='history -a;history -n'
ref: Is it possible to make writing to .bash_history immediate?
ref: Preserve bash history in multiple terminal windows

另外關於 histappend 參數用法可以參考以下連結
ref: Why does history not overwrite but append when histappend is set to off in Bash?

載入修改過後的設定
$ source ~/.bashrc  或  重新開啟一個新的terminal




1

1

LINE 分享

相關創作

[GE] 林頓箱角色「席茲」及「絲蘭」抽抽

【RPG公會】【六座協議_射落王座.實】立場

黑絲襪與肉色絲襪足底分享

留言

開啟 APP

face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】