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
    

Run Remote Commands with SSH

來源 :http://bashcurescancer.com/run_remote_commands_with_ssh.html
-------------------------

Run Remote Commands with SSH

October 25th, 2006

The ssh command is an amazing program. You can use it for opening sessions between server or you can use it run a command on a remote system, non-interactively. A simple example of which might be getting file system usage:

root@admin:~ # ssh root@www 'df -h'
Filesystem Size Used Avail Use% Mounted on
/dev/ubda 3.5G 2.1G 1.4G 61% /
tmpfs 96M 4.0K 96M 1% /dev/shm



This is cool, but is not extremely useful. It gets really cool when you use SSH public/private key pairs to automate tasks on many servers. I will cover that in the future.

Until then, you can still do some pretty cool stuff, like count the number of webserver proccess running on your webserver. Without actually logging into the box and typing the command. All you have to do is place the command in quotes after the base ssh command. The following connects to the server www as root and then pipes the process table to grep apache. It then pipes that to grep -v grep (this eliminates the process grep apache itself and leaves the apache processes) and finally to wc -l which counts the number of lines, in this case the number of apache processes.

root@admin:~ # ssh root@www 'ps -ef | grep apache | grep -v grep | wc -l'
11

Or display a systems vital stats and highest CPU utilizing process:

root@www:~ # ssh root@www 'top -b -n 1 | head -n 8'
top - 11:01:04 up 20 days, 6:47, 1 user, load average: 0.44, 0.21, 0.09
Tasks: 60 total, 1 running, 59 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.7% user, 1.5% system, 0.0% nice, 96.8% idle
Mem: 195620k total, 186276k used, 9344k free, 16380k buffers
Swap: 525304k total, 516k used, 524788k free, 26912k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
 7399 root 19 0 936 936 740 R 9.2 0.5 0:00.20 top

Or see who is logged in on a system:

root@www:~ # ssh root@www 'who'
brock pts/0 Oct 21 10:31 (75.72.194.149)
jim pts/1 Oct 25 06:25 (128.101.163.128)



If you would like to be even more amazed or amused read my article on setting up ssh keys. SSH keys allow you to
skip entering a password, like I did above, when running commands on remote systems.

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

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