Creating Linux alias to enable you work faster.

Linux is a very powerful Operating system and it has many flavors. Its always your choice which one to use but making them work faster and according to your need is something you will need. Here we will talk about alias and write a simple alias file to help you be more productive.Lets see how to Creating Linux alias to enable you work faster.

Creating Linux alias to enable you work faster.
Creating Linux alias to enable you work faster.

Creating Linux alias to enable you work faster.

Alias is like an environment made for you and your commands that made it super easy for you to use the system. Generally alias are kept in root directory with the file named .bash_profile. Lets start with writing the scripts.

Open the file and change it.

vi .bash_profile

After changing the file load the changes by using the below command.

source .bash_profile

First we will write alias for cd commands. Generally what happens is when you are in a hurry you type cdd ccd or c to change directory. We can amend them by below alias.

alias cdd = 'cd'
alias cdd = 'cd'
alias c = 'cd'

Now what will happen is whenever you type any of wrong it will be use as cd only.

Next we will write some alias for git because this is something a software developer use most frequently.

alias gs='git status ' 
alias ga='git add '
alias gb='git branch ' 
alias gc='git commit'
alias gd='git diff'
alias gco='git checkout '

Now writing this shorthand of all this commands will work for you and enables you to work faster.

Also when you are working with git you need to see which branch you are on and what is the status. This is important to keep you safe from any errors. Here use this.

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion                                                                                                                                                                
fi


function_exists() {
    declare -f -F $1 > /dev/null
    return $?
}

for al in `__git_aliases`; do
    alias g$al="git $al"
    
    complete_func=_git_$(__git_aliased_command $al)
    function_exists $complete_fnc && __git_complete g$al $complete_func
done

This will add extra functionality for your git profile. Here you can see this gist for more git alias options and use them.

We sometimes need a simple server to start working for your javascript project. You may not know but you have a simple http python server which you can start anytime and the server will be at your disposal. what you have to do is type below command

python -m SimpleHTTPServer 9999

9999 is the port number. Now lets make an alias for that

alias server='python -m SimpleHTTPServer 9999'

Now whenever you type server as command your http server will start running instantly.

Another thing is you need to monitor the system for which we need either top or htop. Lets write an alias for that

alias tp='htop'

Also you need to change directory and switch back to the last directory here is an alias for that

alias p='pwd'
alias last='cd $pwd'

Now you need to restart your servers say nginx sometimes. Okay lets write it down.

alias rs = 'sudo service nginx restart'

You need to get inside mysql to see your database changes. Why leave this one

alias sql = 'mysql -u username -ppassword'

So we have seen few of the alias that can make your life easy. Here is a compilation of all the above alias.

alias cdd = 'cd'
alias cdd = 'cd'
alias c = 'cd'
alias gs='git status ' 
alias ga='git add '
alias gb='git branch ' 
alias gc='git commit'
alias gd='git diff'
alias gco='git checkout '
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion                                                                                                                                                                
fi


function_exists() {
    declare -f -F $1 > /dev/null
    return $?
}

for al in `__git_aliases`; do
    alias g$al="git $al"
    
    complete_func=_git_$(__git_aliased_command $al)
    function_exists $complete_fnc && __git_complete g$al $complete_func
done
alias server='python -m SimpleHTTPServer 9999'
alias tp='htop'
alias p='pwd'
alias last='cd $pwd'
alias rs = 'sudo service nginx restart'
alias sql = 'mysql -u username -ppassword'

These are minimum set of commands that you can use. There are huge opportunities when you are using alias, you can write commands for your grep, tail/head logs and many other you need to run frequently.

Liked the article please share and subscribe.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.