來源:
GNU/Linux Command-Line Tools Summary
4.2. The command-line history
- Using the command history
- The history command
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 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
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/
」。上下箭咀鍵
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命令紀錄設定
export HISTSIZE=5000
您登出 (logout) 或正常離開 bash 時會將記憶體命令紀錄儲存在環境變數 HISTFILE 所指定的檔案 (預值為 ~/.bash_history) 中。再您下次再執行 bash 或開啟終端機Terminal時就會自動載入以前曾經鍵入過的命令。您亦可以改環境變數 HISTFILESIZE 設定 .bash_history 儲存命令數目,例如:
export HISTFILESIZE=5000
沒有留言:
張貼留言