Linux is one of the most popular operating system among engineers and developers. One of the most popular tool of Linux is its terminal.

It provides lots and lots of commands that makes it extremely fast and convenient to work with Linux.

But having lots command has its own drawback. Each time we need to type a lot of text to make a command work in the terminal.

In this post, we are going to see, how we can create shortcuts for commands in terminal to make our life easy.

Linux Shell

Linux shell provides a command line interface that allows us to give input and execute commands on the operating system.

It is an environment in which we can run our commands, programs, and scripts.

Two type of common shells are:

  1. Bourne – $
  2. C Shell – %

More information about shells is out of scope of this post so we jump directly to Bash (Bourne Again Shell), which is a sub-category of Bourne shell.

Ubuntu terminal uses Bash shell by default.

Shell Script

A shell script is a text file that contains commands that can be run on a terminal.

A shell script is a convenient method of combining lot of commands into one that can be executed by a single command.

For example, we can create a shell script to print current date and time with these steps:
a. Start the terminal
b. Enter following command to create a new file for editing:

nano datetime.sh

c. Enter following commands in the file:

now=$(date +"%T")
echo "Current time : $now"
now=$(date +"%m/%d/%Y")
echo "Current date : $now"

d. Save the file by pressing Ctrl + X (exit) and then Y + Enter (save)
e. Now run the script with the command:

sh datetime.sh

.bashrc

.bashrc is a shell script run by Bash whenever the shell is started. It initialises an interactive shell session.

We can define our variables and aliases in this file that we can use on our shell.

For example, whenever we start our shell, we want to see current date and time.

Execute following commands in order:

a. Move to the home directory first by entering following command in terminal and pressing enter:

cd

b. Open the .bashrc file for editing

nano .bashrc

c. Move to the end of the file and paste following lines:

now=$(date +"%T")
echo "Current time : $now"
now=$(date +"%m/%d/%Y")
echo "Current date : $now"

d. Ctrl + X (exit) , Y + Enter (save)
e. Restart the terminal or re-load .bashrc with the command:

source .bashrc

Now, whenever we will start our terminal, we will see the current date and time.

Creating command aliases

It is not a good practice to pollute the existing .bashrc

Instead, we will create our own file .bash_aliases to define our shortcuts. You can give any other name to the file but this is the most commonly used name. The dot (.) before the file name specifies it is a configuration file and hides it in a normal list command (ls).

After we create this file, we will load this file from .bashrc

Let us create our first command alias:

a. Move to the home directory

cd

b. List all files and check if .bashrc exists in the current directory

ls -al

c. Create the .bash_aliases file

nano .bash_aliases

d. We will create alias for the clear command for now. Enter following line in .bash_aliases file:

alias cls='clear'

e. Now, open .bashrc file and add following lines at the end:

if [ -f !/.bash_aliases ]; then
. ~/.bash_aliases
fi

Above lines will try to load the .bash_aliases file only if it is found.

f. Ctrl + X (exit) , Y + Enter (save)
g. Now, reload the .bashrc file. If there are errors in your lines, it will be printed on the terminal.

source .bashrc

Now, our shortcut is ready to work and we can test it with following command:

cls

My favourite aliases

  1. General commands
    alias ll='ls -l'     (show files as list)
    alias la='ls -al'    (show all files)
    alias df='df -h'     (check how much space is left)
  2. GIT
    alias gs='git status'
    alias gd='git diff'
    alias gl='git log'
    alias ga='git add'
    alias gba='git branch -a'
    alias gc='git checkout'
    alias gcm='git commit -m'
    alias gpl='git pull'
    alias gps='git push'
    alias gm='git merge'
  3. APACHE
    alias webdir='cd /var/www/html'
    alias weblogs='cat /var/log/apache2/error.log'
    alias webstart='sudo service apache2 start'
    alias webstop='sudo service apache2 stop'
    alias webrestart='sudo service apache2 restart'

I hope now you understand how to setup command aliases and make your life easy.

You can go ahead and create more and more aliases of your own.