日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
history命令進(jìn)階使用方法

history 命令用來顯示執(zhí)行過的命令,也可以根據(jù)顯示的命令重新執(zhí)行需要的命令,下面為大家演示一下history 命令進(jìn)階使用方法。

使用 HISTTIMEFORMAT 顯示時(shí)間戳

當(dāng)你從命令行執(zhí)行 history 命令后,通常只會(huì)顯示已執(zhí)行命令的序號(hào)和命令本身,如果你想要查看命令歷史的時(shí)間戳,那么可以執(zhí)行:

# 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

注意:這個(gè)功能只能用在當(dāng) HISTTIMEFORMAT 這個(gè)環(huán)境變量被設(shè)置之后的那些新執(zhí)行的 bash 命令才會(huì)被打上正確的時(shí)間戳。在此之前的所有命令,都將會(huì)顯示成設(shè)置 HISTTIMEFORMAT 變量的時(shí)間。

使用 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)

快速重復(fù)執(zhí)行上一條命令

有 4 種方法可以重復(fù)執(zhí)行上一條命令:

1.使用上方向鍵,并回車執(zhí)行。 2.按 !! 并回車執(zhí)行。 3.輸入 !-1 并回車執(zhí)行。 4.按 Ctrl+P 并回車執(zhí)行。

從命令歷史中執(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)鍵字來執(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
HISTSIZE=450
HISTFILESIZE=450

使用 HISTFILE 更改歷史文件名稱

默認(rèn)情況下,命令歷史存儲(chǔ)在 ~/.bash_history 文件中,添加下列內(nèi)容到 .bash_profile 文件并重新登錄 bash shell,將使用 .commandline_warrior 來存儲(chǔ)命令歷史:

# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

使用 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á)到同樣的效果,而且更簡單。下例中,!^ 從上一條命令獲得第一項(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

使用 HISTSIZE 禁用 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]

分享標(biāo)題:history命令進(jìn)階使用方法
分享地址:http://www.dlmjj.cn/article/dhsjgid.html