成都個(gè)人建網(wǎng)站關(guān)鍵詞優(yōu)化哪家強(qiáng)
目錄
即可即用
Linux不能用向上方向鍵查看歷史命令
history小技巧
即可即用
在用戶的.bash_history(如root用戶~/.bash_history)里面下面添加:
#--------------->加上時(shí)間和執(zhí)行人:?
#記錄執(zhí)行時(shí)間和執(zhí)行者,顯示為21 2019-02-16:09-06-01:edwetl: vi .profile
HISTTIMEFORMAT="%Y-%m-%d:%H-%M-%S:`whoami`: "
#記錄執(zhí)行時(shí)間,顯示為 21 <2019-02-16 09:06:01> : vi .profile
HISTTIMEFORMAT='<%F %T> : '#--------------->關(guān)閉 SSH 窗口后仍然能夠查看以前的命令歷史
#記錄歷史命令的文件
HISTFILE=~/.bash_history
# 新的歷史記錄會(huì)被追加到 ${HISTFILE}文件的末尾
shopt -s histappend
#history -a:每次顯示提示符(即執(zhí)行完命令后),新歷史記錄追加到${HISTFILE}
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"#--------------->保持多少條歷史
# Bash shell 會(huì)話中可以保存的歷史命令數(shù)量
HISTSIZE=1000
#.bash_history 文件中可以保存的歷史命令數(shù)量
HISTFILESIZE=1000
使用! 執(zhí)行歷史命令。
! number 執(zhí)行第幾條命令 (試過,不行)
?顯示最近10條終端執(zhí)行過的命令:
history 10 ?
?搜索歷史:
Ctrl+R
通過指定關(guān)鍵字來(lái)執(zhí)行以前的命令,將執(zhí)行以 ps 打頭的命令:
?!ps
清除整個(gè)命令歷史中的重復(fù)條目:
export HISTCONTROL=erasedups
從命令歷史中剔除連續(xù)重復(fù)的條目:
export HISTCONTROL=ignoredups
將目前history記錄的命令寫入文件:
history? -w? ?/path/record.txt
-r ?將histfiles內(nèi)容讀入到目前shell的history記憶中
Linux不能用向上方向鍵查看歷史命令
可能是history命令被禁用了。使用echo $HISTSIZE指令查看輸出值是否等于0,為0則按照問題解決方法步驟解決。執(zhí)行
set -o | grep history
如果返回history off,則在?/.bashrc的末尾添加以下行:
set -o history
執(zhí)行 source?~/.bashrc 使其生效
接著嘗試:
echo $HISTFILE? ? ? ? ? #?HISTFILE變量定義了命令歷史列表保存在哪個(gè)文件中
echo $HISTSIZE? ? ? ? ?#命令輸出的記錄數(shù),即.bash_history文件中的最后HISTSIZE行
echo $HISTFILESIZE? #HISTFILESIZE 定義了在 .bash_history 中保存命令的記錄總數(shù)
如果第一個(gè)為空或/dev/null,請(qǐng)將此行添加到?/ .bashrc的末尾:
HISTFILE=$HOME/.bash_history
如果最后兩個(gè)中的任何一個(gè)打印為0,則將它們?cè)O(shè)置為某個(gè)數(shù)字,例如默認(rèn)值500:
HISTFILESIZE=500
HISTSIZE=500
bash內(nèi)置history命令之HISTFILE變量-szbryan-ChinaUnix博客
HISTFILESIZE與HISTSIZE的區(qū)別 - 瘋狂110 - 博客園
環(huán)境變量$HISTSIZE:默認(rèn)值是1000,也就是最多存1000條命令。
環(huán)境變量$HISTFILE:默認(rèn)值是/home/登錄用戶/.bash_history。root用戶是/root/.bash_history。bash進(jìn)程退出后,把內(nèi)存里的命令歷史存放到此文件中。
環(huán)境變量$HISTFILESIZE:默認(rèn)值是1000。命令歷史文件的大小。
history小技巧
1.history命令
使用! 執(zhí)行歷史命令。
! number 執(zhí)行第幾條命令 (試過,不行)
! command 從最近的命令查到以command開頭的命令執(zhí)行(別忘了中間空格)
!! 執(zhí)行上一條(試過,不行)
vi /etc/profile
?HISTSIZE=2000 ? ? ? ? //定義history 命令輸出的記錄數(shù)
?HISTTIMEFORMAT="%Y-%m-%d:%H-%M-%S:`whoami`: ?" ? ?//記錄每條歷史命令的執(zhí)行時(shí)間和執(zhí)行者,顯示結(jié)果為21 ?2019-02-16:09-06-01:edwetl: ?vi .profile
?HISTTIMEFORMAT='<%F %T> : ' ? //顯示結(jié)果為 ?21 ?<2019-02-16 09:06:01> : vi .profile
3.history的歷史命令保存在~/.bash_history 文件中.
linux 查看歷史命令 history命令 - 千里走單琦 - 博客園
使用 HISTTIMEFORMAT 顯示時(shí)間戳
執(zhí)行:export HISTTIMEFORMAT='%F %T '?讓history歷史記錄前帶時(shí)間戳,格式如下
# export HISTTIMEFORMAT='%F %T ' # history | more 1 2008-08-05 19:02:39 service network restart 2 2008-08-05 19:02:39 exit 3 2008-08-05 19:02:39 id 4 2008-08-05 19:02:39 cat /etc/redhat-release
export HISTTIMEFORMAT="%F %T " 等價(jià) export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"
%F: 表示完整的日期,格式為 YYYY-MM-DD。
%T: 表示 24 小時(shí)制的時(shí)間,格式為 HH:MM:SS。
?持久化:
將 export HISTTIMEFORMAT="%F %T " 參數(shù)添加到當(dāng)前用戶的~/.bashrc文件 或者在全局的/etc/profile文件最后加入
# source ~/.bashrc 命令使其生效
使用 Ctrl+R 搜索歷史
Ctrl+R 是我經(jīng)常使用的一個(gè)快捷鍵,此快捷鍵讓你對(duì)命令歷史進(jìn)行搜索,對(duì)于想要重復(fù)執(zhí)行某個(gè)命令的時(shí)候非常有用。當(dāng)找到命令后,通常再按回車鍵就可以執(zhí)行pre該命令,如果想對(duì)找到的命令進(jìn)行調(diào)整后再執(zhí)行,則可以按一下左或右方向鍵。
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] (reverse-i-search)`red‘: cat /etc/redhat-release [Note: Press enter when you see your command, which will execute the command from the history] # cat /etc/redhat-release Fedora release 9 (Sulphur)
從命令歷史中執(zhí)行一個(gè)指定的命令
在下面的例子中,如果你想重復(fù)執(zhí)行第 4 條命令,那么可以執(zhí)行 !4:
# history | more 1 service network restart 2 exit 3 id 4 cat /etc/redhat-release # !4 cat /etc/redhat-release Fedora release 9 (Sulphur)
通過指定關(guān)鍵字來(lái)執(zhí)行以前的命令
在下面的例子,輸入 !ps 并回車,將執(zhí)行以 ps 打頭的命令:
# !ps ps aux | grep yp root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp
使用 HISTSIZE 控制歷史命令記錄的總行數(shù)
將下面兩行內(nèi)容追加到 .bash_profile 文件并重新登錄 bash?shell,命令歷史的記錄數(shù)將變成 450 條:
# vi ~/.bash_profile# Bash shell 會(huì)話中可以保存的歷史命令數(shù)量 HISTSIZE=1000 #.bash_history 文件中可以保存的歷史命令數(shù)量 HISTFILESIZE=1000
寫入~/.bashrc
文件,該用戶的操作歷史。寫入/etc/profile
,整個(gè)系統(tǒng)。
使用 HISTFILE 更改歷史記錄保存文件
默認(rèn)情況下,命令歷史存儲(chǔ)在 ~/.bash_history 文件中,可以修改
# vi ~/.bash_profile HISTFILE=/var/log/history_cmd.txt
使用 HISTCONTROL 從命令歷史中剔除連續(xù)重復(fù)的條目
在下面的例子中,pwd 命令被連續(xù)執(zhí)行了三次。執(zhí)行 history 后你會(huì)看到三條重復(fù)的條目,要剔除這些重復(fù)的條目,你可以將 HISTCONTROL 設(shè)置為 ignoredups:
# pwd # pwd # pwd # history | tail -4 44 pwd 45 pwd 46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above] 47 history | tail -4 # export HISTCONTROL=ignoredups # pwd # pwd # pwd # history | tail -3 56 export HISTCONTROL=ignoredups 57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above] 58 history | tail -4
使用 HISTCONTROL 清除整個(gè)命令歷史中的重復(fù)條目
上例中的 ignoredups 只能剔除連續(xù)的重復(fù)條目,要清除整個(gè)命令歷史中的重復(fù)條目,可以將 HISTCONTROL 設(shè)置成 erasedups:
# export HISTCONTROL=erasedups # pwd # service httpd stop # history | tail -3 38 pwd 39 service httpd stop 40 history | tail -3 # ls -ltr # service httpd stop # history | tail -6 35 export HISTCONTROL=erasedups 36 pwd 37 history | tail -3 38 ls -ltr 39 service httpd stop [Note that the previous service httpd stop after pwd got erased] 40 history | tail -6
使用 HISTCONTROL 強(qiáng)制 history 不記住特定的命令
將 HISTCONTROL 設(shè)置為 ignorespace,并在不想被記住的命令前面輸入一個(gè)空格:
# export HISTCONTROL=ignorespace # ls -ltr # pwd # service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history] # history | tail -3 67 ls -ltr 68 pwd 69 history | tail -3
使用 -c 選項(xiàng)清除所有的命令歷史
如果你想清除所有的命令歷史,可以執(zhí)行:
# history -c
命令替換
在下面的例子里,!!:$ 將為當(dāng)前的命令獲得上一條命令的參數(shù):
# ls anaconda-ks.cfg anaconda-ks.cfg # vi !!:$ vi anaconda-ks.cfg
補(bǔ)充:使用 !$ 可以達(dá)到同樣的效果,而且更簡(jiǎn)單。下例中,!^ 從上一條命令獲得第一項(xiàng)參數(shù):
# cp anaconda-ks.cfg anaconda-ks.cfg.bak anaconda-ks.cfg # vi -5 !^ vi anaconda-ks.cfg
為特定的命令替換指定的參數(shù)
在下面的例子,!cp:2 從命令歷史中搜索以 cp 開頭的命令,并獲取它的第二項(xiàng)參數(shù):
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt # ls -l !cp:2 ls -l /really/a/very/long/path/long-filename.txt
下例里,!cp:$ 獲取 cp 命令的最后一項(xiàng)參數(shù):
# ls -l !cp:$ ls -l /really/a/very/long/path/long-filename.txt
禁用 history
如果你想禁用 history,可以將 HISTSIZE 設(shè)置為 0:
# export HISTSIZE=0 # history # [Note that history did not display anything]
使用 HISTIGNORE 忽略歷史中的特定命令
下面的例子,將忽略 pwd、ls、ls -ltr 等命令:
# export HISTIGNORE=”pwd:ls:ls -ltr:” # pwd # ls # ls -ltr # service httpd stop # history | tail -3 79 export HISTIGNORE=”pwd:ls:ls -ltr:” 80 service httpd stop 81 history [Note that history did not record pwd, ls and ls -ltr]
寫入~/.bashrc
文件,該用戶的操作歷史。寫入/etc/profile
,整個(gè)系統(tǒng)。