亚洲高清vr播放在线观看,欧美亚洲精品免费,欧美日韩天堂在线视频,午夜福利小视频

      學習啦 > 學習電腦 > 操作系統(tǒng) > Linux教程 > Linux中history命令怎么用

      Linux中history命令怎么用

      時間: 孫勝652 分享

      Linux中history命令怎么用

        Linux中history命令用于查看已使用過的命令,在Linux系統(tǒng)操作中經(jīng)常會用到,也是初學者必須要掌握的命令,下面學習啦小編就給大家介紹下Linux中history命令的實例,以便你有個了解。

        1.使用 HISTTIMEFORMAT 顯示時間戳

        當你從命令行執(zhí)行 history 命令后,通常只會顯示已執(zhí)行命令的序號和命令本身。如果你想要查看命令歷史的時間戳,那么可以執(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

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

        2.使用 Ctrl+R 搜索歷史

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

        3.快速重復執(zhí)行上一條命令

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

        使用上方向鍵,并回車執(zhí)行。

        按 !! 并回車執(zhí)行。

        輸入 !-1 并回車執(zhí)行。

        按 Ctrl+P 并回車執(zhí)行。

        4.從命令歷史中執(zhí)行一個指定的命令

        在下面的例子中,如果你想重復執(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)

        5.通過指定關鍵字來執(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

        6.使用 HISTSIZE 控制歷史命令記錄的總行數(shù)

        將下面兩行內(nèi)容追加到 .bash_profile 文件并重新登錄 bash shell,命令歷史的記錄數(shù)將變成 450 條:

        代碼如下:

        # vi ~/.bash_profile

        HISTSIZE=450

        HISTFILESIZE=450

        7.使用 HISTFILE 更改歷史文件名稱

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

        代碼如下:

        # vi ~/.bash_profile

        HISTFILE=/root/.commandline_warrior

        8.使用 HISTCONTROL 從命令歷史中剔除連續(xù)重復的條目

        在下面的例子中,pwd 命令被連續(xù)執(zhí)行了三次。執(zhí)行 history 后你會看到三條重復的條目。要剔除這些重復的條目,你可以將 HISTCONTROL 設置為 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

      312236