2011年4月26日 星期二

Using the command history---取回以前曾經鍵入過的命令

來源:


GNU/Linux Command-Line Tools Summary
   Prev    Chapter 4. Shell Tips    Next

4.2. The command-line history

  • Using the command history
  •    
    Use the up and down key's to scroll through previously typed commands. Press [Enter] to execute them or use the left and right arrow keys to edit the command first. Also see history(below).
  • The history command
  •    
    The history command can be used to list Bash's log of the commands you have typed:
    This log is called the “history”. To access it type:
       
    
    history n
    
    This will only list the last n commands. Type “history” (without options) to see the the entire history list.
    You can also type !n to execute command number n. Use !! to execute the last command you typed.
    !-n will execute the command n times before (in other words !-1 is equivalent to !! ).
    !string will execute the last command starting with that “string” and !?string? will execute the last command containing the word “string”. For example:
       
    
    !cd
    
    Will re-run the command that you last typed starting with “cd”.
    “ commandName !*” will execute the “commandName” with any arguments you used on your last command. This maybe useful if you make a spelling mistake, for example. If you typed:
       
    
    emasc /home/fred/mywork.java /tmp/testme.java
    
    In an attempt to execute emacs on the above two files this will obviously fail. So what you can do is type:
       
    
    emacs !*
    
    This will execute emacs with the arguments that you last typed on the command-line. In other words this is equivalent to typing:
       
    
    emacs /home/fred/mywork.java /tmp/testme.java
    
  • Searching through the Command History ( CTRL -R )
  •    
    Use the CTRL-R key to perform a “reverse-i-search”. For example, if you wanted to use the command you used the last time you used snort , you would type:
    CTRL -R then type “snort”.
    What you will see in the console window is:
       
    
    (reverse-i-search)'':
    
    After you have typed what you are looking for, use the CTRL -R key combination to scroll backward through the history.
    Use CTRL -R repeatedly to find every reference to the string you've entered. Once you've found the command you're looking for, use [Enter] to execute it.
    Alternatively, using the right or left arrow keys will place the command on an actual command-line so you can edit it. -----------------------------------------------------------------------------------------

    取回以前曾經鍵入過的命令

    目錄

     [隱藏]

    history

    很多 shell 都會把您曾經鍵入過的命令記錄在記憶體中,您可以用指令 history 列出來:
    $ history
      1  ls
      2  mount /media/sd_mmc
      3  ls /media/sd_mmc
      4  cd ..
      5  dir
      6  dcim/
      7  ls
      8  vdir
      9  cd ..
     10  cd Images/
     11  ls
     12  cd ..
     13  cd
     14  umount /media/sd_mmc/
     15  cd
     16  ls -lhSr
     17  rm core
     18  df -h
     19  cd photo
     20  ls
    
    以上列表中,左邊為命令編數,右邊為命令本身。您每鍵入一個命令,Shell 都會記下來並指定命令編數為上一個命令加一。

     !n

    您可以打「!命令編數」直接執行。例如您打「!16」,Shell 就會直接執行編號第 16 的命令「ls -lhSr」。

     !-n

    如果命令編數為負數,表示要執行對上幾次的命令。例如您打「!-3」,Shell 就會直接執行倒數第三個命令(即編號第 18 的命令)「df -h」。

     !!

    打「!!」等於「!-1」,表示執行倒數第一個命令(即編號第 20 的命令)「ls」。

     !?

    打「!?關鍵字」會執行對上一個包括關鍵字的命令。例如「!?mmc」,Shell 會找最後一次包括「mmc」的命令,即是編數第 14 的命令「umount /media/sd_mmc/」。

    上下箭咀鍵

    現時有部份 shell 包括 bash(一般 GNU/Linux 預設)、tcsh 和 zsh 等都可以用上下箭咀鍵取回以前曾經鍵入過的命令。

    Ctrl-R

    按 Ctrl-R 打鍵入關鍵字,shell會尋找上一次包括關鍵字的命令。例如按 Ctrl-R 打鍵入「m」,Shell會顯最後一個包含「m」的命令:「rm core」。
    再鍵入「m」,Shell 會顯示最後一個包含「mm」的命令:「umount /media/sd_mmc/」。
    再按一次 Ctrl-R,shell 會顯示對上次包含相同關鍵字「mm」的命令:「ls /media/sd_mmc/」。再按一次 Ctrl-R,會顯示再對上次包含相同關鍵字「mm」的命令:「mount /media/sd_mmc/」。

    Bash命令紀錄設定

    環境變數 HISTSIZE 可以用來設定 bash 最多會記錄命令的數目,例如:
    export HISTSIZE=5000
    
    您登出 (logout) 或正常離開 bash 時會將記憶體命令紀錄儲存在環境變數 HISTFILE 所指定的檔案 (預值為 ~/.bash_history) 中。再您下次再執行 bash 或開啟終端機Terminal時就會自動載入以前曾經鍵入過的命令。您亦可以改環境變數 HISTFILESIZE 設定 .bash_history 儲存命令數目,例如:
    export HISTFILESIZE=5000
    

沒有留言:

如何在Windows的事件檢視器event viewer,查詢開機關機的紀錄

Windows 如何 事件檢視器 查詢開機關機紀錄 Step1: 打開windows的 「 事件檢視器 」,開啟「 建立自訂檢視… 」視窗,在「事件記錄檔」選項,將「Windows 紀錄」內勾選「系統」選項。 Step2: 內含/排除事件識別碼: 」排除事件識別碼打「6005,6...