日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux中Shell历史命令记录文件的路径是什么

發(fā)布時(shí)間:2024/4/17 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux中Shell历史命令记录文件的路径是什么 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

Bash shell在“~/.bash_history”(“~/”表示用戶目錄)文件中保存了500條使用過(guò)的命令,這樣能使你輸入使用過(guò)的長(zhǎng)命令變得容易。每個(gè)在系統(tǒng)中擁有賬號(hào)的用戶在他的目錄下都有一個(gè)“.bash_history”文件。bash shell應(yīng)該保存少量的命令,并且在每次用戶注銷(xiāo)時(shí)都把這些歷史命令刪除。 第一步: “/etc/profile”文件中的“HISTFILESIZE”和“HISTSIZE”行確定所有用戶的“.bash_history”文件中能保 存的舊命令條數(shù)。強(qiáng)烈建議把把“/etc/profile”文件中的“HISTFILESIZE”和“HISTSIZE”行的值設(shè)為一個(gè)較小的數(shù),比如 30。編輯profile文件(vi /etc/profile),把下面這行改為: HISTFILESIZE=30 HISTSIZE=30 這表示每個(gè)用戶的“.bash_history”文件只能保存30條舊命令。 第二步: 網(wǎng)管還應(yīng)該在"/etc/skel/.bash_logout" 文件中添加下面這行"rm -f $HOME/.bash_history" 。這樣,當(dāng)用戶每次注銷(xiāo)時(shí),“.bash_history”文件都會(huì)被刪除.

?

?

http://www.huanxiangwu.com/137/linux-history%E5%91%BD%E4%BB%A4%E4%BB%8B%E7%BB%8D

現(xiàn)在大多數(shù)的Linux系統(tǒng)都使用bash作為默認(rèn)的shell吧,下面就介紹一下bash的history命令管理功能吧,history命令可以回顧,修改和重用之前使用過(guò)的歷史命令。

1.一些變量說(shuō)明:

$HISTFILE bash啟動(dòng)的時(shí)候會(huì)讀取~/.bash_history文件并載入到內(nèi)存中,這個(gè)變量就用于設(shè)置.bash_history文件,bash退出時(shí)也會(huì)把內(nèi)存中的歷史回寫(xiě)到.bash_history文件

$HISTSIZE 設(shè)置bash會(huì)員期間歷史包含的命令數(shù)量

$HISTFILESIZE 設(shè)置歷史文件中實(shí)際存儲(chǔ)的命令數(shù)量

2.顯示歷史命令

history 顯示全部歷史

history 數(shù)字 顯示之前執(zhí)行過(guò)的若干命令,例:history 2 顯示執(zhí)行過(guò)的上兩條命令

使用上下箭頭鍵也可以查看上一條根下一條命令,

3.運(yùn)行歷史命令

!! 運(yùn)行上一條命令

!88 運(yùn)行第88條命令

!88 /test 運(yùn)行第88條命令并在命令后面加上/test

!?CF? 運(yùn)行上一個(gè)包含CF字符串的命令

!ls 運(yùn)行上一個(gè)ls命令

!ls:s/CF/l 運(yùn)行上一個(gè)ls命令,其中把CF替換成l

fc 編輯并運(yùn)行上一個(gè)歷史命令

fc 66 編輯并運(yùn)行第66個(gè)歷史命令

fc -e /usr/bin/vim 66 使用vim編輯第66個(gè)命令并運(yùn)行

4.搜索歷史命令

使用ctrl+r搜索歷史中的字符串,重復(fù)按ctrl+r可以在歷史命令列表中不斷的向前搜索包含字符串的命令,回車(chē)就會(huì)執(zhí)行查找的命令

5.清空歷史命令

history -c

6.寫(xiě)history

history -w 讓bash將歷史命令立即從內(nèi)存寫(xiě)到.bash_history文件

history -a 將目前新增的 history 歷史命令寫(xiě)入.bash_history文件

7.history歷史命令記錄刪除

修改/etc/profile將HISTSIZE=1000改成0或1

清除用戶home路徑下.bash_history

8.history配置

運(yùn)行 set | grep HISTFILE

顯示:HISTFILE=/root/.bash_history
HISTFILESIZE=1000

在.bash_profile文件中添加

HISTFILE=/root/history

export HISTFILE

重新登錄后歷史命令都會(huì)寫(xiě)入到/root/history文件中

其余的一些設(shè)置可以在.bashrc文件中設(shè)置

export HISTCONTROL=ignoredups #忽略重復(fù)的命令

export HISTIGNORE=”[ ]*:&:bg:fg:exit” #忽略由冒號(hào)分割的這些命令

export HISTFILESIZE=1000 #設(shè)置保存的歷史命令的文件大小

export HISTSIZE=100 #設(shè)置保存的歷史命令的條數(shù)

技巧:

shopt -s histappend 在shell中執(zhí)行這個(gè)命令可以使shell保存歷史命令的時(shí)候使用追加的方式,因?yàn)槟J(rèn)是覆蓋,在多終端的清空下,最后退出的終端灰覆蓋以前的歷史記錄

在history歷史記錄中顯示時(shí)間和執(zhí)行命令的用戶 echo ‘export HISTTIMEFORMAT=”%F %T `whoami` “‘ >> /etc/profile

?

http://linuxtoy.org/archives/history-command-usage-examples.html

?

History(歷史)命令用法 15 例

如果你經(jīng)常使用 Linux 命令行,那么使用 history(歷史)命令可以有效地提升你的效率。本文將通過(guò)實(shí)例的方式向你介紹 history 命令的 15 個(gè)用法。

使用 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í)間。[感謝 NightOwl 讀者補(bǔ)充]

使用 Ctrl+R 搜索歷史

Ctrl+R 是我經(jīng)常使用的一個(gè)快捷鍵。此快捷鍵讓你對(duì)命令歷史進(jìn)行搜索,對(duì)于想要重復(fù)執(zhí)行某個(gè)命令的時(shí)候非常有用。當(dāng)找到命令后,通常再按回車(chē)鍵就可以執(zhí)行該命令。如果想對(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í)行上一條命令:

  • 使用上方向鍵,并回車(chē)執(zhí)行。
  • 按 !! 并回車(chē)執(zhí)行。
  • 輸入 !-1 并回車(chē)執(zhí)行。
  • 按 Ctrl+P 并回車(chē)執(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ò)指定關(guān)鍵字來(lái)執(zhí)行以前的命令

    在下面的例子,輸入 !ps 并回車(chē),將執(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ǔ)在 ~/.bashhistory 文件中。添加下列內(nèi)容到 .bashprofile 文件并重新登錄 bash shell,將使用 .commandline_warrior 來(lái)存儲(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á)到同樣的效果,而且更簡(jiǎn)單。[感謝 wanzigunzi 讀者補(bǔ)充]

    下例中,!^ 從上一條命令獲得第一項(xiàng)參數(shù):

    # cp anaconda-ks.cfg anaconda-ks.cfg.bak anaconda-ks.cfg # vi -5 !^ vi anaconda-ks.cfg

    為特定的命令替換指定的參數(shù)

    在下面的例子,!cp:2 從命令歷史中搜索以 cp 開(kāi)頭的命令,并獲取它的第二項(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]

    ?

    總結(jié)

    以上是生活随笔為你收集整理的linux中Shell历史命令记录文件的路径是什么的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。