Bash shell shortcuts that every system administrator must know

Sarath Pillai's picture
bash shell shortcut keys

The default shell that most of the Linux distribution provides is bash. Bash stands for Bourne Again Shell. Being a system administrator you need to work with bash in day to day life. Increasing the knowledge of the shell commands and its scripting and programming functionalities will make life much easier.

In this tutorial we will be seeing some interesting shortcuts that you can use while working on any Linux system. The list mentioned here are some of my favourites as well as i believe is a must know for all system administrators out there. Without the knowledge of these shortcuts and some command line tricks, users will find it difficult or sometimes select the long path to achieve certain task.

This article will not make you a pro user of bash (for learning bash and its scripting capabilities i will be publishing a series of tutorial posts in the coming days), however it will surely make your life easier at terminal.

1. Move cursor to different places in command line.

You might have faced situations where you have already typed a long command with multiple switches and arguments, and want to modify something in the beginning of the command, or something in the middle of the command, or something at the end of the command.

These things can be done by moving the cursor to different parts of the typed command.

Lets begin this by moving forward and backward by one word in the command line.

  • ALT + B : Move Backward by one word at a time
  • ALT + F : Move Forward by one word at a time

You can easily remember this by keeping B & F in mind

Let’s now see how to move the cursor to the beginning and end of the command.

  • CTRL + A: Move curser to beginning of the command
  • CTRL + E: Move curser to the end of the command

 

2.Delete typed command fully or partly

At times you need to delete one word from your command, or want to remove several words from your command, or sometimes remove the entire command altogether.

You can achieve this in bash by using the following shortcut keys.

  • CTRL + W: Delete one word at a time moving backwards
  • CTRL + U: Delete the entire command at once

You can also delete half or part your command by first moving the curser to your required location and then pressing 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.

5. 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.

6. 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.

 

7. 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

8. 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

 

9. 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

 

10. 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.

 

11. 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

 

12. 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.

 

13. 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.
Rate this article: 
Average: 4.2 (12 votes)

Comments

Thanks for the post

Hi,

In order to move to user's home directory use the following

cd ~username instead of using cd /home/username

Thanks
Aravindan R

Very helpful post , we need some more post like this .

Thankyou

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.