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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,125评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,293评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,054评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,077评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,096评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,062评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,988评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,817评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,266评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,486评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,646评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,375评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,974评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,621评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,642评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,538评论 2 352

推荐阅读更多精彩内容