shell shortcut

大多数Linux发行版中默认的shell为bash. Bash是Bourne Again Shell的缩写。使用快捷键可以极大提升工作效率。

1. 移动光标

ALT + B : 向前移动一个单词
ALT + F : 向后移动一个单词
CTRL + B:向前移动一个字母
CTRL + F:向后移动一个字母
CTRL + A: 移动到命令起始处
CTRL + E: 移动到命令结尾处

2. 删除

CTRL + W: 向左删一个单词
CTRL + U: 删除光标至起始处全部字符
CTRL + K: 删除光标后面的所有字符
3.Auto completion and searching command
I must say that this is the highly pressed and used key by any administrator while they are using a bash terminal. There is a reason for this.
There are thousands of commands in a Linux machine, and this number goes on increasing as you go on installing programs. It becomes very much difficult for any system administrator to remember all the commands (except a handful of them which are remembered due to daily usage). Bash terminal helps you to toggle and search commands by using the TAB key
Type one word or few letters that your command begins with, and then press TAB the shell will show you the matching commands on terminal.
Also this feature is more helpful in completing a log file name, which otherwise requires you to enter the exact file name.
4.Using ! and !! in bash
While you work with bash shell, sometimes you need to type a previously typed command once again. Or say you want to rerun a particular command that you typed a couple of minutes or hours back.
Pressing UP arrow key and searching for your required command might take a long time. However there is a faster method to achieve this, if you remember the first or first couple of letters of that command.
** !<first alphabet or first few alphabet of your command.>**
However, if you give a double exclamation mark (!!), then it will execute the last command you executed.

  1. Switch to previous directory
    **CD – **Will change the directory to the previous directory where you were. So for example if you were in the directory **/var/log/httpd/, **and then you change your directory to /home.
    Now if you run this command from /home, you will reach your previous directory (/var/log/httpd).
    Also again rerunning this command at /var/log/httpd will change your directory back to
    /home

    Basically this command will change your current working directory to the previous one.

  2. Saving commands in the history without running them
    You can save commands in the history without actually executing it by adding a # in the beginning of the command.
    Now for example, see the below command.

  3. Clearing the screen
    Normally people use the command “clear” to clear the screen. Although that’s a good command to remember, there is another interesting method to clear the screen by leaving the last typed command.
    This can be done by using **CTRL + L **and then ENTER

  4. Cutting and pasting in bash
    This is a very rarely used shortcut key(mostly because people are not aware of this.). In the section “Delete Typed Command Fully or partly”, we have seen methods to delete words , some parts of the command, or the entire command altogether.
    In fact the operation done while you delete, using **CTRL + U, CTRL + W, CTRL + K **is “cutting”.
    After you delete anything in your command line with the above shown switches you can paste whatever you have deleted with the command
    **CTRL +Y: **Paste things deleted using CTRL + U, CTRL +W, CTRL + K

  5. Convert to upper case and lower case letters
    You can convert lower case letters in your command to upper case letters, as well as upper case letters to lower case by a simple key stroke.
    Place your cursor at the beginning of the word you want to convert to (upper or lower case), and then press the below keys
    ALT + U: This will make the word (starting from the cursor) upper case
    ALT + L: This will make the word (starting from the cursor) Lower case

  1. Searching History using Bash
    A couple of minutes back we have seen two commands “!” and “!!”. They allow you to search previous command you typed based on some starting strings you provide, or run the previous command typed. These commands are used to search bash command line history that stores all your typed command.
    There are some interesting switches that can be used with these two commands. Let’s see them one by one.
    As discussed previously “!<letters>” will search for last command the begins with the letters you provided. However if you just want to see the exact command that
    “!<letters>”
    would execute, you can print that command on screen without executing it with the below method.
    !<first few letters of the command>:p

**:p **switch would print the command, without executing it.
Also if you want the last word of your previously executed command, then you can get that with the following shortcut key
**!$ **get the last word of the previous command (you can also use the :p switch to print the last word instead of executing it)

The above seen shortcuts to search history will not let you to go through each command you typed. There are methods that are very commonly used by administrators that will let you travel through your bash history. Let’s see some of them here.
**CTRL + R **This will give you a searching interface where you can type keywords contained in any of of your previous command.

You can see the searching being done, and press and enter to execute on finding your command.
**CTRL + G **This switch will make you exit from the history searching interface.

Alternatively you can travel through commands in the history both backward and forward using the below switches.
**CTRL + P **Travel backwards in history one by one.
**CTRL + N **Travel forward in history one by one.

**Note: **The above can be achieved by simply pressing the UP and DOWN arrow as well.

  1. Stop Commands and output to be printed on screen
    You can stop the command and its outputs being printed to the screen by using the below switches.
    CTRL +S This will stop the output to screen
    **CTRL + Q **This will again start allowing output being printed on screen
  1. Truncating a file
    You can easily make a large file empty with one simple command in bash. This would truncate the file to zero size, without modifying anything other than emptying it.
    This becomes very much handy for truncating log files in automated scripts.
    >filename

The above will truncate the file to zero size.

  1. Terminating and suspending a command
    This is the most commonly used bash shortcuts. I must say newbie’s use this all the time. Because if you want to terminate something, that’s currently running then this shortcut becomes handy.
    **CTRL + C **This will terminate the currently running process. This sends a SIGINIT signal to the currently running process.
    **CTRL + Z **This will suspend the currently running process in the shell.
    原文作者:flyingsoul
    原文地址: https://www.jianshu.com/p/5367ca646a59
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞